思路:可以bfs来写,也可以用思维来解
代码:思维代码
void solve(){cin >> n;for(int i = 0;i < 2;i ++)cin >> s[i];for(int i = 0;i < 2;i ++){// cout << s[i] << endl;for(int j = 0;j < n;j ++){if(i == 0){if(((j + 1) % 2 == 0) && s[i][j] == '<' && (s[i + 1][j - 1] == '<' || s[i + 1][j + 1] == '<')){cout << "NO\n";return;}}else{if(((j + 1) & 1) && s[i][j] == '<' && (s[i - 1][j - 1] == '<' || s[i - 1][j + 1] == '<')){cout << "NO\n";return;}}}}cout << "YES\n";
}
bfs代码
bool bfs()
{memset(st,0,sizeof st);queue<pair<int,int>> q;q.push({1,1});st[1][1]=true;while(q.size()){auto t=q.front();q.pop();for(int i=0;i<4;i++){int tx=t.first+dx[i],ty=t.second+dy[i];if(tx<1||ty<1||tx>2||ty>n) continue;st[tx][ty]=true;if(x[tx][ty]=='<') ty-=1;else ty+=1;if(tx<1||ty<1||tx>2||ty>n) continue;if(st[tx][ty]) continue;st[tx][ty]=true;if(st[2][n]) return true;q.push({tx,ty});}}return false;
}
int main()
{int t;cin>>t; while(t--){cin>>n;for(int i=1;i<=2;i++)for(int j=1;j<=n;j++)cin>>x[i][j];if(bfs()) cout<<"YES"<<endl;else cout<<"NO"<<endl;}return 0;
}