fztt_r9 @ 2023-09-03 11:49:42
using namespace std;
bool unacc[114][114];//走不了
int m,n,f=0;
void dfs(int x,int y){
if(f) return ;
// cout<<x<<' '<<y<<endl;
if(x==n&&y==m){
cout<<"YES"<<endl;
f=1;
return;
}
if(!(x<=n&&x>=1&&y<=m&&y>=1)) return;
if(!unacc[x][y+1]){
unacc[x][y+1]=1;
dfs(x,y+1);
unacc[x][y+1]=0;
}
if(!unacc[x+1][y]){
unacc[x+1][y]=1;
dfs(x+1,y);
unacc[x+1][y]=0;
}
if(!unacc[x-1][y]){
unacc[x-1][y]=1;
dfs(x-1,y);
unacc[x-1][y]=0;
}
if(!unacc[x][y-1]){
unacc[x][y-1]=1;
dfs(x,y-1);
unacc[x][y-1]=0;
}
return ;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int k=1;k<=m;k++){
char ch;
cin>>ch;
if(ch=='#') unacc[i][k]=1;
}
}
// for(int i=1;i<=n;i++){
// for(int k=1;k<=m;k++){
// cout<<unacc[i][k]<<' ';
// }
// cout<<endl;
//}
dfs(1,1);
if(f==0) cout<<"NO"<<endl;
}
by fztt_r9 @ 2023-09-03 18:42:51
为啥没人鸟我
by Cgetier01 @ 2023-09-08 20:53:00
Yes和No写错了,其次每一个判断后不需要回溯,把unacc[x][y+1]=0;这个删掉就行了。
by fztt_r9 @ 2023-09-14 13:33:57
@Cgetier01 感谢!关注了