Pipes
You are given a system of pipes. It consists of two rows, each row consists of n pipes. The top left pipe has the coordinates (1,1) and the bottom right — (2,n).
There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types:
You can turn each of the given pipes 9090 degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types 1 and 2 can become each other and types 3,4,5,6 can become each other).
You want to turn some pipes in a way that the water flow can start at (1,0) (to the left of the top left pipe), move to the pipe at (1,1), flow somehow by connected pipes to the pipe at (2,n) and flow right to (2,n+1).
Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes:
Let’s describe the problem using some example:
And its solution is below:
As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at (1,2) 90 degrees clockwise, the pipe at (2,3) 90 degrees, the pipe at (1,6) 90 degrees, the pipe at (1,7) 180 degrees and the pipe at (2,7)180 degrees. Then the flow of water can reach (2,n+1) from (1,0).
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1≤q≤10^4) — the number of queries. Then q queries follow.
Each query consists of exactly three lines. The first line of the query contains one integer n (1≤n≤2⋅10^5) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of n digits from 1 to 6 without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes.
It is guaranteed that the sum of n over all queries does not exceed 2⋅10^5.
Output
For the i-th query print the answer for it — “YES” (without quotes) if it is possible to turn some pipes in a way that the water flow can reach (2,n+1) from (1,0), and “NO” otherwise.
Example
Input
6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54
Output
YES
YES
YES
NO
YES
NO
Note
The first query from the example is described in the problem statement.
思路:(个人觉得这道题出的很好,看了大佬思路超级牛逼啊~)
1.首先有两行,对于每一列,不同的管子类型有:
1和2类型的可以从该列直接到右边的一列
其余的类型只能到达该列的另一行
因为是从左边过来的,所以3,4,5,6类型的不能到右边
2.我们判断能不能从该列到下一列来判断是否可行,因为我们就是按照列
从左到右模拟,然后行随着管子类型变换,最后看是否为第二行即可
(具体可看代码实现)
代码如下:
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int mx=2e5+10;
int t,n;
string s[2];
void solve(){
int cur=0;
for(int i=0;i<n;i++){
if(s[cur][i]=='1'||s[cur][i]=='2') continue;
if(s[cur^1][i]>='3'&&s[cur^1][i]<='6') cur^=1;//变换行
else{
printf("NO\n");
return;
}
}
if(cur==1) printf("YES\n");
else printf("NO\n");
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
cin>>s[0]>>s[1];
solve();
}
return 0;
}