CodeForces-1234C-Pipes-dfs

You are given a system of pipes. It consists of two rows, each row consists of nn pipes. The top left pipe has the coordinates (1,1)(1,1) and the bottom right — (2,n)(2,n).git

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:ide

Types of pipes

You can turn each of the given pipes 9090 degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types 11 and 22 can become each other and types 3,4,5,63,4,5,6 can become each other).spa

You want to turn some pipes in a way that the water flow can start at (1,0)(1,0) (to the left of the top left pipe), move to the pipe at (1,1)(1,1), flow somehow by connected pipes to the pipe at (2,n)(2,n) and flow right to (2,n+1)(2,n+1).3d

Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes:code

Examples of connected pipes

Let's describe the problem using some example:blog

The first example input

And its solution is below:three

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)(1,2) 9090 degrees clockwise, the pipe at (2,3)(2,3) 9090 degrees, the pipe at (1,6)(1,6) 9090 degrees, the pipe at (1,7)(1,7) 180180 degrees and the pipe at (2,7)(2,7) 180180degrees. Then the flow of water can reach (2,n+1)(2,n+1) from (1,0)(1,0).ip

You have to answer qq independent queries.ci

Input

The first line of the input contains one integer qq (1q1041≤q≤104) — the number of queries. Then qq queries follow.字符串

Each query consists of exactly three lines. The first line of the query contains one integer nn (1n21051≤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 nndigits from 11 to 66 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 nn over all queries does not exceed 21052⋅105.

Output

For the ii-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)(2,n+1) from (1,0)(1,0), and "NO" otherwise.

 

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.

 

 題意:

給出t組數據,每組數據給出兩組長度爲n的字符串,拼接起來表明一個長爲n寬爲2的長方形,水(0,0)進入,從右下角那個格子橫着流出。若能從開頭流出去,輸出YES,不然輸出NO。

水管有如下幾種類型,能夠90度旋轉任意次,由於能夠經過旋轉獲得,因此一、2能夠看做是A類型,3-6能夠看做是B類型。

 

 

思路:

首先判斷起點是什麼類型的水管,進行dfs,本身定義四個方向(上下左右),開始進行dfs。dfs(x,y,ss),x、y表明傳入的下標,表示當前走到的點,ss表示當前的流向,而後再對當前流向所能到達的那個點

進行一下流向判斷,判斷其能流到哪裏去。

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int flag,n;  5 char a[2][200020];  6 
 7 //1左、2下、3右、4上
 8 
 9 void dfs(int x,int y,int ss)//從a[x][y]流過來,方向ss
10 { 11     if(y>=n) 12         return; 13     if(x==1&&y==n-1&&ss==3)//x、y表示已經走到右下角了,ss=3表示是橫着流出去的,符合題意
14  { 15         flag=1; 16         return; 17  } 18     if(x==0)//從上一行流過來的,方向只能是向下或者向右
19  { 20         if(ss==2)//判斷的流過來的方向是怎麼樣的,向下流過來的
21  { 22             if(a[x+1][y]!='1'&&a[x+1][y]!='2') 23                 dfs(x+1,y,3); 24  } 25         else if(ss==3)//向右流過來的
26  { 27             if(a[x][y+1]=='1'||a[x][y+1]=='2') 28                 dfs(x,y+1,3); 29             else dfs(x,y+1,2); 30  } 31  } 32     else if(x==1)//從下一行流過來的,方向只能是向上或者向右
33  { 34         if(ss==4)//
35  { 36             if(a[x-1][y]!='1'&&a[x-1][y]!='2') 37                 dfs(x-1,y,3); 38  } 39         else if(ss==3)//
40  { 41             if(a[x][y+1]=='1'||a[x][y+1]=='2') 42                 dfs(x,y+1,3); 43             else
44                 dfs(x,y+1,4); 45  } 46  } 47 } 48 
49 int main() 50 { 51     int t; 52     scanf("%d",&t); 53     while(t--) 54  { 55         scanf("%d",&n); 56         scanf("%s %s",a[0],a[1]); 57         flag=0; 58         if(a[0][0]=='1'||a[0][0]=='2')//只能向右走
59             dfs(0,0,3); 60         else//否則只能向下走
61             dfs(0,0,2); 62         if(flag) 63             printf("YES\n"); 64         else
65             printf("NO\n"); 66  } 67     return 0; 68 }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息