作者:
ベ断桥烟雨ミ_53
,
2023-02-01 10:33:51
,
所有人可见
,
阅读 4
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
const int N=210;
char a[N][N];
int dist[N][N];
void bfs(PII start)
{
queue<PII> q;
q.push(start);
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
while(!q.empty())
{
PII u=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=u.x+dx[i],y=u.y+dy[i];
if(a[x][y]=='#')continue;
if(a[x][y]=='.')
{
dist[x][y]=dist[u.x][u.y]+1;
a[x][y]='#';
q.push({x,y});
}
if(a[x][y]=='E')
{
cout<<dist[u.x][u.y]+1<<endl;
return;
}
}
}
cout<<"oop!"<<endl;
}
int main(){
int t;
cin>>t;
while(t--)
{
memset(a,'#',sizeof a);
memset(dist,0,sizeof dist);
int r,c;
cin>>r>>c;
PII start;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
start.x=i,start.y=j;
}
bfs(start);
}
return 0;
}