做这题的时候是真的感觉到自己被耍了。
原本想要练习一下dfs来到了codeforces寻找dfs的题目,然后发现了这题看起来很简单,就是以前做过的迷宫,然后直接dfs就开始做了。最终写了一大堆东西写出了这个dfs,超时掉了。然后我也不知道这个是错的还是对的,可能是对的吧,写得这么工整。哎,虽然思路错掉了但是这也算是我第一次思路清晰地写出来dfs。
#include<bits/stdc++.h>
using namespace std;
int dx[8] = {-1,-1,0,1,1,1,0,-1};
int dy[8] = {0,-1,-1,-1,0,1,1,1};
const int N = 200;
int g[2][N];
bool st[2][N];
int n;
bool dfs(int ti,int tj){
if(ti==1&&tj==n-1){
return true;
}
for(int k = 0;k < 8 ;k ++){
int ii=ti + dx[k];
int jj=tj + dy[k];
if((!st[ii][jj])&&ii<2&&ii>=0&&jj<n&&jj>=0&&g[ii][jj]!=1){
st[ii][jj] = true;
if(dfs(ii,jj)==true){
return true;
}
st[ii][jj] = false;
}
}
return false;
}
int main(){
int t;
cin>>t;
while(t--){
memset(g,0,sizeof g);
memset(st,false,sizeof st);
cin>>n;
for(int i = 0; i < 2;i ++){
for(int j = 0; j < n ; j ++){
scanf("%1d",&g[i][j]);
}
}
if(dfs(0,0)==true){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
}