#P1518. Catch the Escaped Cows

    ID: 14804 Type: Default 1000ms 256MiB

Catch the Escaped Cows

Catch the Escaped Cows

Farmer John is chasing two runaway cows into a forest. The chase takes place on a 10 × 10 grid. Each grid cell may contain one of the following characters:

  • . for open field,
  • * for an obstacle,
  • C for the cows (they always stay together),
  • F for Farmer John.

Both Farmer John and the cows begin by facing north, and every minute they take an action simultaneously as follows:

If the cell in the direction they are currently facing is not blocked by an obstacle or by the edge of the grid (note that the edge is considered blocked), they move one cell forward in that same direction. Otherwise, they do not move but instead turn 90° clockwise. (They will never leave the grid.)

The chase ends if, at the end of any minute, Farmer John and the cows occupy the same cell. (Passing each other during the move does not count.) If they never meet, output 0.

The movement decision is based solely on the grid configuration, so both Farmer John and the cows follow the exact same rules. Your task is to simulate their behavior and determine how many minutes it takes for Farmer John to catch the cows.

In LaTeX, the grid size and turning angle can be noted as:

Grid size: \(10 \times 10\).
Turning angle: \(90^\circ\) clockwise.

inputFormat

The input consists of 10 lines. Each line contains exactly 10 characters representing the grid. The characters will be one of ., *, C, or F. There is exactly one F and exactly one C in the grid, and they will not start in the same cell.

outputFormat

Output a single integer: the number of minutes until Farmer John and the cows occupy the same cell at the end of a minute. If they never meet, output 0.

sample

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