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: conicoc1
LANG: C
TASK: ttwo
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char Maps[12][12];
void Rotate(int *Location)
{
Location[2]++;
if(Location[2]==5)
Location[2]=1;
}
void Imitate(int *Location)
{
switch(Location[2])
{
case 1:if(Maps[Location[0]-1][Location[1]]=='.')
Location[0]--;
else
Rotate(Location);
break;
case 2:if(Maps[Location[0]][Location[1]+1]=='.')
Location[1]++;
else
Rotate(Location);
break;
case 3:if(Maps[Location[0]+1][Location[1]]=='.')
Location[0]++;
else
Rotate(Location);
break;
case 4:if(Maps[Location[0]][Location[1]-1]=='.')
Location[1]--;
else
Rotate(Location);
break;
}
}
int main()
{
FILE *fin,*fout;
fin=fopen("ttwo.in","r");
fout=fopen("ttwo.out","w");
int Farmer[3];
int Cow[3];
int i,j,count=0;
for(i=1;i<=10;i++){
for(j=1;j<=10;j++){
fscanf(fin,"%c",&Maps[i][j]);
if(Maps[i][j]=='F')
Farmer[0]=i,Farmer[1]=j,Maps[i][j]='.';
else if(Maps[i][j]=='C')
Cow[0]=i,Cow[1]=j,Maps[i][j]='.';
}
fscanf(fin,"\n");
}
Farmer[2]=1;
Cow[2]=1;
while(Cow[0]!=Farmer[0]||Cow[1]!=Farmer[1]){
Imitate(Cow);
Imitate(Farmer);
count++;
if(count>160000)
break;
}
count>160000?fprintf(fout,"0\n"):fprintf(fout,"%d\n",count);
return 0;
}
关键是那个100*4 * 100*4 超过160000个情况肯定会有重复,比较难理解