题目传送门
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
char g[N][N];
//北,东,南,西
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
//读入地图
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cin >> g[i][j];
//找到牛的位置,农夫的位置
int x1, y1, x2, y2;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++) {
if (g[i][j] == 'C') x1 = i, y1 = j;
if (g[i][j] == 'F') x2 = i, y2 = j;
}
//方向,0:北,1:东,2:南,3:西
int cd = 0, fd = 0; //牛的方向,农夫方向
//假设牛和 Farmer John 一开始的行动方向都是正北(即上)
int cnt = 0; //几分钟
while (true) {
//模拟牛转90度方向+走一步
if (x1 + dx[cd] >= 10 || x1 + dx[cd] < 0
|| y1 + dy[cd] >= 10 || y1 + dy[cd] < 0
|| g[x1 + dx[cd]][y1 + dy[cd]] == '*')
cd = (cd + 1) % 4; //换方向
else x1 += dx[cd], y1 += dy[cd];//走一步
//模拟农夫转90度方向+走一步
if (x2 + dx[fd] >= 10 || x2 + dx[fd] < 0
|| y2 + dy[fd] >= 10 || y2 + dy[fd] < 0
|| g[x2 + dx[fd]][y2 + dy[fd]] == '*')
fd = (fd + 1) % 4;
else x2 += dx[fd], y2 += dy[fd];
//分钟数增加
cnt++;
//找到啦~
if (x1 == x2 && y1 == y2) {
cout << cnt << endl;
break;
}
//次数太多也找不到
if (cnt > 1000000) {
cout << 0 << endl;
break;
}
}
return 0;
}