代码,需要的人得看题目的要求改代码
#include<bits/stdc++.h>
#include<queue>
int a,b;
using namespace std;
char mp[525][525];
int flag=0;
int dx[5]={-1,1,0,0,},dy[5]={0,0,-1,1};
int book[525][525]={};
struct po{
int x;
int y;
};
int bfs(int x,int y) {
queue<po> s;
s.push({x,y});
po hd;
while(s.size()>0){
hd=s.front();
s.pop();
if(mp[hd.x][hd.y]=='g') {
return true;
}
for(int l=0;l<4;l++) {
int nx=hd.x+dx[l],ny=hd.y+dy[l];
if(nx>0&&ny>0&&nx<=a&&ny<=b&&mp[nx][ny]!='#'&&book[nx][ny]!=1) {
s.push({nx,ny});
book[nx][ny]=1;
}
}
}
return false;
}
int main() {
cin>>a>>b;
for(int l=1;l<=a;l++) {
cin>>mp[l]+1;
}
for(int l=1;l<=a;l++){
for(int i=1;i<=b;i++) {
if(mp[l][i]=='s') {
flag=bfs(l,i);
}
}
}
cout<<(flag?"Yes":"No");
return 0;
}