1. 程式人生 > >USACO2.4.1 The Tamworth Two 兩隻塔姆沃斯牛 解題報告(模擬)

USACO2.4.1 The Tamworth Two 兩隻塔姆沃斯牛 解題報告(模擬)

2.4.1 The Tamworth Two 兩隻塔姆沃斯牛

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 18  Solved: 14 [Submit][Status][Discuss]

Description

兩隻牛在森林裡故意走丟了。農民John開始用他的專家技術追捕這兩頭牛。你的任務是模擬他們的行為(牛和John)。 追擊在10x10的平面網格內進行。一個格子可以是: 一個障礙物, 兩頭牛(它們總在一起), 或者 農民John. 兩頭牛和農民John可以在同一個格子內(當他們相遇時),但是他們都不能進入有障礙的格子。 一個格子可以是: . 空地 * 障礙物 C 兩頭牛 F 農民John 這裡有一個地圖的例子::

*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......
 

牛在地圖裡以固定的方式遊蕩。每分鐘,它們可以向前移動或是轉彎。如果前方無障礙且不會離開地圖,它們會按照原來的方向前進一步。否則它們會用這一分鐘順時針轉90度。 農民John, 深知牛的移動方法,他也這麼移動。 每次(每分鐘)農民John和兩頭牛的移動是同時的。如果他們在移動的時候穿過對方,但是沒有在同一格相遇,我們不認為他們相遇了。當他們在某分鐘末在某格子相遇,那麼追捕結束。開始時,John和牛都面向北方。

Input

Lines 1-10: 每行10個字元,表示如上文描述的地圖。

Output

輸出一個數字,表示John需要多少時間才能抓住牛們。輸出0,如果John無法抓住牛。

Sample Input

*...*.....
......*...
...*...*..
..........
...*.F....
*.....*...
...*......
..C......*
...*.*....
.*.*......

Sample Output

49

從F和C點向上走,如果不能走則順時針轉向90°,問需要多久兩點重合

直接模擬就好,設一個大數值讓其到達此數值跳出,否則相遇時跳出

程式碼如下

#include <map>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#define lowbit(a) (a&(-a))
#define _mid(a,b) ((a+b)/2)
#define debu(a) cout << a <<" ";
#define debug(a) cout << a << endl;
#define _mem(a,b) memset(a,0,(b+3)<<2)
#define fori(a) for(int i=0;i<a;i++)
#define forj(a) for(int j=0;j<a;j++)
#define ifor(a) for(int i=1;i<=a;i++)
#define jfor(a) for(int j=1;j<=a;j++)
#define mem(a,b) memset(a,b,sizeof(a))
#define IN freopen("in.txt","r",stdin)
#define OUT freopen("out.txt","w",stdout)
#define IO do{\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);}while(0)
#define mp(a,b) make_pair(a,b);
using namespace std;
typedef long long ll;
const int maxn =  15;
const int INF = 0x3f3f3f3f;
const int inf = 0x3f;
const double EPS = 1e-7;
const double Pi = acos(-1);
const int MOD = 1e9+7;
char g[maxn][maxn];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
struct node{
    int x,y,m;
}a,b;
bool juge(int x,int y){return x&&x<=10&&y&&y<=10&&g[x][y]!='*';}
 
void move(node &a){
    if(!juge(a.x+dir[a.m][0],a.y+dir[a.m][1]))
        a.m = (a.m+1)%4;
    else
        a.x+=dir[a.m][0],a.y+=dir[a.m][1];
}
 
void getres(){
    int cnt = 0;
    while(cnt < 5000&&++cnt){
        move(a);
        move(b);
        if(a.x==b.x&&a.y==b.y)
            break;
    }
    cout << (cnt==5000?0:cnt)<<endl;
}
 
int main() {
    int n = 10;
    a.m = b.m = 0;
 
    for(int i=10;i>0;i--)
        jfor(10){
            cin >> g[i][j];
            if(g[i][j]=='F')
                a.x = i,a.y = j;
            else if(g[i][j] == 'C')
                b.x = i,b.y = j;
        }
    getres();
    return 0;
}