USACO Section 2.4 The Tamworth Two
BIO '98 - Richard Forster
A pair of cows is loose somewhere in the forest. Farmer John is lending his expertise to their capture. Your task is to model their behavior.
The chase takes place on a 10 by 10 planar grid. Squares can be empty or they can contain:
- an obstacle,
- the cows (who always travel together), or
- Farmer John.
The cows and Farmer John can occupy the same square (when they `meet') but neither the cows nor Farmer John can share a square with an obstacle.
Each square is
|
Here is a sample grid:
*...*..... ......*... ...*...*.. .......... ...*.F.... *.....*... ...*...... ..C......* ...*.*.... .*.*...... |
The cows wander around the grid in a fixed way. Each minute, they either move forward or rotate. Normally, they move one square in the direction they are facing. If there is an obstacle in the way or they would leave the board by walking `forward', then they spend the entire minute rotating 90 degrees clockwise.
Farmer John, wise in the ways of cows, moves in exactly the same way.
The farmer and the cows can be considered to move simultaneously during each minute. If the farmer and the cows pass each other while moving, they are not considered to have met. The chase ends when Farmer John and the cows occupy the same square at the end of a minute.
Read a ten-line grid that represents the initial state of the cows, Farmer John, and obstacles. Each of the ten lines contains exactly ten characters using the coding above. There is guaranteed to be only one farmer and one pair of cows. The cows and Farmer John will not initially be on the same square.
Calculate the number of minutes until the cows and Farmer John meet. Assume both the cows and farmer begin the simulation facing in the `north' direction. Print 0 if they will never meet.
PROGRAM NAME: ttwo
INPUT FORMAT
Lines 1-10: | Ten lines of ten characters each, as explained above |
SAMPLE INPUT (file ttwo.in)
*...*..... ......*... ...*...*.. .......... ...*.F.... *.....*... ...*...... ..C......* ...*.*.... .*.*......
OUTPUT FORMAT
A single line with the integer number of minutes until Farmer John and the cows meet. Print 0 if they will never meet.
SAMPLE OUTPUT (file ttwo.out)
49
题意:在一个10*10的地图中FJ要扑捉逃走的cow,FJ和cow在地图中行走的规则是一样的,规则是每分钟都向前行走一步,如果前面是墙则顺时针旋转90度也需要一分钟,当FJ和cow在同一个格子里是就算FJ已经成功扑捉cow了,输出扑捉的步数,如果永远都扑捉不到就输出0.
分析:对于FJ的所以状态 10*10*4 = 400,一共有400种状态;同样cow也400,所以总共的步数为400*400.超过这个就是永远捕捉不到。
小技巧:如果像我平常一样写搜索FJ和cow每个有 4 种情况,我就得写 16 个判断。这样很容易出错。p[4][2]={{-1,0},{0,1},{1,0},{-1,0}};也就是p[0]、p[1]、p[2]、p[3]分别是向上、向右、向下、向左。这样处理旋转只需 加 1 模 4 就行了。
心得:这种方法我以前很少用,总觉得判断不会很多都是些复制粘贴碰见最多的也8个判断,这次却来了18种,所以以后还是用这种方法吧。

/* ID: dizzy_l1 LANG: C++ TASK: ttwo */ #include<iostream> #include<cstdio> #include<queue> #define MAXN 15 using namespace std; int max_ans=400*400; int dx[4]={-1,0,1,0};//小技巧 int dy[4]={0,1,0,-1}; char map[MAXN][MAXN]; struct point { int x,y,p; }f,c; void getnext(point &t) { t.x+=dx[t.p];t.y+=dy[t.p]; if(map[t.x][t.y]=='*') { t.x-=dx[t.p];t.y-=dy[t.p]; t.p=(t.p+1)%4;//顺时针转90度 } } int Search() { int ans=0; while(ans<=max_ans) { if(f.x==c.x&&f.y==c.y) return ans; getnext(f); getnext(c); ans++; } return 0; } int main() { freopen("ttwo.in","r",stdin); freopen("ttwo.out","w",stdout); int i,j; for(i=1;i<=10;i++) { map[0][i]=map[11][i]=map[i][0]=map[i][11]='*'; for(j=1;j<=10;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='F') { f.x=i;f.y=j;f.p=0; } else if(map[i][j]=='C') { c.x=i;c.y=j;c.p=0; } } getchar(); } printf("%d\n",Search()); return 0; }