#include <iostream>
#include <string>
using namespace std;
int n, m;
string maze[110];
bool vis[110][110];
int dir[4][2] = {{-1, 0},{0, -1},{1, 0},{0, 1}};
int ans = 100000000;
bool in(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
void dfs(int x, int y, int step)
{
if (maze[x][y] == 'T')
{
if (step < ans)
{
ans = step;
}
return;
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++)
{
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty])
{
dfs(tx, ty, step + 1);
}
}
vis[x][y] = 0;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> maze[i];
}
int x, y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maze[i][j] == 'S') {
x = i, y = j;
}
}
}
dfs(x, y, 0);
cout << ans << endl;
return 0;
}
迷宫最短路
最新推荐文章于 2022-06-12 23:34:52 发布