有一点推简单了
2024-09-07 17:26:55
发布于:广东
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
char a[105][105];
int dx[4] = {1,-1,0,0},dy[4] = {0,0,1,-1},vis[105][105];
struct node{
int x,y,step;
};
int main(){
queue<node> q;
int n,m,sx,sy;
cin >> n >> m;
for(int i = 0;i < n;i ++){
for(int j = 0;j < m;j ++){
cin >> a[i][j];
if(a[i][j] == 'S'){
sx = i;
sy = j;
}
}
}
q.push({sx,sy,0});
while(!q.empty()){
int ix = q.front().x;
int iy = q.front().y;
int istep = q.front().step;
q.pop();
if(a[ix][iy] == 'T'){
cout << istep;
return 0;
}
for(int i = 0;i < 4;i ++){
int nx = ix + dx[i];
int ny = iy + dy[i];
if(nx >= 0 and ny >= 0 and nx < n and ny < m and vis[nx][ny] == 0 and a[nx][ny] != '#'){
vis[nx][ny] = 1;
q.push({nx,ny,istep + 1});
}
}
}
cout << -1;
return 0;
}
这里空空如也
有帮助,赞一个