模拟 + dfs
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
char mp[10][10];
PII Mydistance[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int t = 0;
int x, y, a, b;
void dfs(int x, int y, int x1, int y1, int i, int j)
{
int flag = 0;
if(x == x1 && y1 == y) return;
if(t > 400) return;
int tempx = x + Mydistance[i % 4].first;
int tempy = y + Mydistance[i % 4].second;
int tempx1 = x1 + Mydistance[j % 4].first;
int tempy1 = y1 + Mydistance[j % 4].second;
if(tempx < 0 || tempx >= 10 || tempy < 0 || tempy >= 10 || mp[tempx][tempy] == '*') i ++, flag += 2;
if(tempx1 < 0 || tempx1 >= 10 || tempy1 < 0 || tempy1 >= 10 || mp[tempx1][tempy1] == '*') j ++, flag -= 3;
t ++;
if(flag == 2)
{
dfs(x, y, tempx1, tempy1, i, j);
}
else if(flag == -3)
{
dfs(tempx, tempy, x1, y1, i, j);
}
else if(flag == -1)
{
dfs(x, y, x1, y1, i, j);
}
else if(flag == 0)
{
dfs(tempx, tempy, tempx1, tempy1, i, j);
}
}
int main()
{
for(int i = 0; i < 10; i ++)
for(int j = 0; j < 10; j ++)
{
cin >> mp[i][j];
if(mp[i][j] == 'F') a = i, b = j;
if(mp[i][j] == 'C') x = i, y = j;
}
dfs(x, y, a, b, 0, 0);
if(t <= 400) cout << t;
else cout << 0;
return 0;
}
PII存4个方向, 地图大小是100 如果走超过400步算找不到。flag判断障碍情况