2.4.1 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
/* ID: makeeca1 PROG: ttwo LANG: C++ */ #include<cstdio> #include<cstring> char map[12][12]; int k1,k2,x1,x2,y1,y2,flag; int dx[5]={-1,0,1,0}; int dy[5]={0,1,0,-1}; int check(){ if (x1==x2 && y1==y2)return 1; return 0; } int move(int &x,int &y,int &k){ x+=dx[k];y+=dy[k]; if (map[x][y]=='*'){ x-=dx[k];y-=dy[k]; k=(k+1)%4; } } void dfs(int step){ if (flag)return; if (step>160000){ if (check())printf("%d\n",step);else printf("0\n"); flag=1; return; } if (check()){ printf("%d\n",step); flag=1; return; } move(x1,y1,k1); move(x2,y2,k2); // printf("%d %d %d\n%d %d %d\n\n",x1,y1,k1,x2,y2,k2); dfs(step+1); } int main(){ freopen("ttwo.in","r",stdin); freopen("ttwo.out","w",stdout); memset(map,0,sizeof(map)); for (int i=1;i<=10;i++){ for (int j=1;j<=10;j++){ map[i][j]=getchar(); if (map[i][j]=='F'){x1=i;y1=j;} if (map[i][j]=='C'){x2=i;y2=j;} } getchar(); } for(int i=0;i<=11;i++) map[i][0]=map[0][i]=map[11][i]=map[i][11]='*'; k1=k2=0;flag=0; dfs(0); return 0; }