在解决这个问题时,我用了 bfs。
#include<bits/stdc++.h>
bool c[500][500],f[500][500][3];
char g[750000];
short h,w,a[750000],b[750000],x,y,m,n;
int s=0,t=1;
void r(int o){
if(o&&(g[t]=g[s]+c[x][y])^3&&!f[x][y][g[t]])if(x^m||y^n)f[a[t]=x][b[t]=y][g[t]]=1,t++;else puts("YES"),exit(0);
}
int main(){
scanf("%hd%hd",&h,&w);
for(short i=0;i<h;i++){
getchar();
for(short j=0;j<w;j++){
char _=getchar();
if(_^35){
c[i][j]=0;
if(_^'s'){if(_^46)m=i,n=j;}
else f[a[0]=i][b[g[0]=0]=j][0]=1;
}
else c[i][j]=1;
}
}
while(s<t)x=a[s]-1,y=a[t],r(~x),r((x+=2)^h),x--,r(~--y),r((y+=2)^w),f[a[s]][b[s]][g[s]]=0,s++;
puts("NO");
}
我不断地交了这段代码,但它总过不了测试点 2。大家知道这段代码错在哪儿了吗?
说实话,这是我第 3 次发这个帖了,所以只要你有帮我调代码的意愿(即使没有调好),也可以回复。