C. Pipes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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:
Types of pipes
You can turn each of the given pipes 90 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:
Examples of connected pipes
Let’s describe the problem using some example:
The first example input
And its solution is below:
The first example answer
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≤104) — 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⋅105) — 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⋅105.
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
inputCopy
6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54
outputCopy
YES
YES
YES
NO
YES
NO
Note
The first query from the example is described in the problem statement.
题解:dfs,详细的我全部写注释了
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10005;
int m,n;
int flag;
char s[2][N];
void dfs(int x,int y,int op) //op代表走着哪条路出发。
{
if(y>=m) return;
if(x==1&&y==m-1&&op==3) {flag=1;return ;}//这个位置已经在第二行m-1位置上,只要往右走即可到达(2,m);
if(x==0)//在第一行,只有往下走或者往右走
{
if(op==3) //往右走
{
if(s[x][y+1]=='1'||s[x][y+1]=='2') dfs(x,y+1,3); //如果(x,y+1)的位置上是1和2管子型,就继续往右走
else dfs(x,y+1,2); //否则只能往下走
}
else if(op==2) //往下走
{
if(s[x+1][y]!='1'&&s[x+1][y]!='2') dfs(x+1,y,3);//因为在第一行往下走,那么(x+1,y)只能往右走。
}
}
else if(x==1) //在第二行只能往上走或者往右走。
{
if(op==3)
{
if(s[x][y+1]=='1'||s[x][y+1]=='2') dfs(x,y+1,3);//如果右边是1和2管子那就继续往右走。
else dfs(x,y+1,1);//否则我们只能往上走.
}
else if(op==1)
{
if(s[x-1][y]!='1'&&s[x-1][y]!='2') dfs(x-1,y,3);//因为是往上走的,偶们只能选择一种。(x-1,y)只能往右走。
}
}
}
int main()
{
cin>>n;
while(n--)
{
flag=0;
cin>>m;
cin>>s[0];
cin>>s[1];//1向上走,2向下走,3向右走
if(s[0][0]=='1'||s[0][0]=='2') dfs(0,0,3); //因为水是从左边进来的。如果是1和2管子的话,只能往右走。
else dfs(0,0,2);//如果不是1和2管子的话,我们必定是往下走的。
if(flag) puts("YES");
else puts("NO");
}
return 0;
}