CodeForces-1234C-파이프-DFS

당신은 파이프 시스템을 제공하고 있습니다. 그것은 두 개의 행으로 구성되어, 각 행의 구성  N N 파이프. 왼쪽 상단 파이프 좌표 갖는  ( 1 , 1 ) - (1,1) 및 하부 우측  ( 2 , N ) (2, N)를.

직선 파이프의 두 가지 유형 및 곡선 파이프의 네 가지 유형 : 파이프의 여섯 종류가 있습니다. 다음은 모두 6 가지 유형의 예입니다 :

파이프의 종류

사용자는 주어진 파이프에 각각 회전 수  90 개 90 도로 시계 방향 또는 반 시계 방향으로  , 임의의가 (아마도, 제로) 시간 (수 있도록 유형  12 2는 서로 종류가 될 수있는  3 , 4 , 5 , 6 3,4,5 6)은 서로가 될 수있다.

당신은 물 흐름을 개시 할 수있는 방식으로 일부 파이프를 설정하려면  ( 1 , 0 ) (1,0) (왼쪽 상단 파이프의 왼쪽)에서 파이프로 이동  ( 1 , 1 ) (1, 1)으로 흘러 든  에서 파이프에 접속 된 파이프  ( 2 , N ) (2, n)은 오른쪽으로 흐른다  ( 2 , N + 1 ) ) (2, N + 1.

그들은 시스템의 인접과 끝이 연결되어있는 경우 파이프가 연결되어 있습니다. 여기에 연결 파이프의 예이다 :

연결 파이프의 예로

의 몇 가지 예를 사용하여 문제를 설명하자 :

제 1 실시 입력

그리고 그 해결책은 다음과 같습니다 :

첫 번째 예제 응답

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).

You have to answer qq independent queries.

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度旋转任意次,因为可以通过旋转得到,所以1、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 }

 

추천

출처www.cnblogs.com/OFSHK/p/11620296.html