题解
2025-08-06 16:03:08
发布于:河北
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int n,m;
char a[118][118];
bool b[118][118];
int f[4][2]={-1,0,1,0,0,1,0,-1};
struct st
{
int x,y,l;
};
queue <st> q;
bool pd(int x,int y)
{
return x>0&&y>0&&x<=n&&y<=m&& a[x][y]!='#' && !b[x][y];
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
{
q.push({i,j,0});
b[i][j]=1;
}
}
}
while(!q.empty())
{
st t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int nx=t.x+f[i][0];
int ny=t.y+f[i][1];
if(pd(nx,ny))
{
if(a[nx][ny]=='T')
{
cout<< t.l+1 <<endl;
return 0;
}
b[nx][ny]=1;
q.push({nx,ny,t.l+1});
}
}
}
cout<< -1 <<endl;
return 0;
}
这里空空如也
有帮助,赞一个