#include<bits/stdc++.h>
using namespace std;
char a[205][205];
int go[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
int t,r,c,sx,sy,flag;
struct zb{
int x;
int y;
int step;
};
void bfs(int x,int y,int step)
{
zb s,n;
s.x=x;
s.y=y;
s.step=step;
queue<zb> mg;
mg.push(s);
while(!mg.empty())
{
s=mg.front();
for(int i=0;i<4;i++)
{
n.x=s.x+go[i][0];
n.y=s.y+go[i][1];
n.step=s.step+1;
if(a[n.x][n.y]=='E')
{
cout<<n.step<<endl;
return ;
}
if(n.x>0&&n.x<=r&&n.y>0&&n.y<=c&&a[n.x][n.y]=='.')
{
a[n.x][n.y]=='#';
mg.push(n);
}
}
mg.pop();
}
cout<<"oop!"<<endl;
return ;
}
int main()
{
cin>>t;
while(t--)
{
cin>>r>>c;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
cin>>a[i][j];
if(a[i][j]=='S')
{
sx=i;
sy=j;
}
}
}
bfs(sx,sy,0);
}
return 0;
}
这份代码在从S到E有路时可以输出,但出现没路可走的情况下没办法输出“oop!”,一直卡死,求助!