# USACO 2.4 The Tamworth Two 两只塔姆沃斯牛 解题报告

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
represented
as follows:

. Empty square
* Obstacle
C Cows
F Farmer
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


. 空地

• 障碍物
C 两头牛
F 农民John
这里有一个地图的例子：

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

牛在地图里以固定的方式游荡。每分钟，它们可以向前移动或是转弯。如果前方无障碍且不会离开地图，它们会按照原来的方向前进一步。否则它们会用这一分钟顺时针转90度。

PROGRAM NAME: ttwo

INPUT FORMAT

SAMPLE INPUT (file ttwo.in)

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

OUTPUT FORMAT

SAMPLE OUTPUT (file ttwo.out)
49

======================== 华丽的分割线========================

C语言:

/*
LANG: C
ID: zqy11001
PROG: ttwo
*/
#include <stdio.h>
#define changemap(a, c) if(map[i][j] == c){\\
a[0] = i;\\
a[1] = j;\\
map[i][j] = \'.\';\\
}

char way1[4] = {-1, 0, 1, 0};
char way2[4] = {0, 1, 0, -1};
char map[11][11];
char f[4], c[4];

void move(char *n)
{
int i, j;
i = n[0] + way1[n[2]];
j = n[1] + way2[n[2]];
if(i > 10 || i < 1 || j > 10 || j < 1 || map[i][j] == \'*\'){
n[2] = (n[2] + 1) % 4;
}else{
n[0] = i;
n[1] = j;
}
}

int main(void)
{
int i, j;
freopen(ttwo.in, r, stdin);
freopen(ttwo.out, w, stdout);
for(i = 1; i <= 10; i++){
for(j = 1; j <= 10; j++){
scanf(%c, &map[i][j]);
changemap(f, \'F\');
changemap(c, \'C\');
}
getchar();
}
for(i = 0; i < 160000 && (f[0] != c[0] || f[1] != c[1]); i++){
move(f);
move(c);
}
printf(%d\\n, i%160000);
return 0;
}`