模擬算法:
#include<iostream>
using namespace std;
char land[11][11];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = { 0, 1, 0, -1};
int cx, cy, fx, fy, fdir, cdir, ans;
bool check(int x, int y, int dir) {
x = x + dx[dir], y = y + dy[dir];
return x >= 1 && x <= 10 && y >= 1 && y <= 10 && land[x][y] != '*';
}
int main() {
for(int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
cin >> land[i][j];
if (land[i][j] == 'F') {
fx = i, fy = j;
}
if(land[i][j] == 'C') {
cx = i, cy = j;
}
}
}
for (int i = 1; i <= 1000000; i++) {
if (fx == cx && fy == cy) {
cout << ans << endl;
return 0;
}
if (check(fx,fy,fdir)) {
fx = fx + dx[fdir];
fy = fy + dy[fdir];
} else {
fdir = (fdir+1) % 4;
}
if (check(cx,cy,cdir)) {
cx = cx + dx[cdir];
cy = cy + dy[cdir];
} else {
cdir = (cdir+1) % 4;
}
ans++;
}
cout << 0 << endl;
return 0;
}