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

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

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

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)(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 (1≤q≤1041≤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 (1≤n≤2⋅1051≤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 2⋅1052⋅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表示当前的流向,然后再对当前流向所能到达的那个点

进行一下流向判断,判断其能流到哪里去。

 #include<stdio.h>
#include<string.h> int flag,n;
char a[][]; //1左、2下、3右、4上 void dfs(int x,int y,int ss)//从a[x][y]流过来,方向ss
{
if(y>=n)
return;
if(x==&&y==n-&&ss==)//x、y表示已经走到右下角了,ss=3表示是横着流出去的,符合题意
{
flag=;
return;
}
if(x==)//从上一行流过来的,方向只能是向下或者向右
{
if(ss==)//判断的流过来的方向是怎么样的,向下流过来的
{
if(a[x+][y]!=''&&a[x+][y]!='')
dfs(x+,y,);
}
else if(ss==)//向右流过来的
{
if(a[x][y+]==''||a[x][y+]=='')
dfs(x,y+,);
else dfs(x,y+,);
}
}
else if(x==)//从下一行流过来的,方向只能是向上或者向右
{
if(ss==)//上
{
if(a[x-][y]!=''&&a[x-][y]!='')
dfs(x-,y,);
}
else if(ss==)//右
{
if(a[x][y+]==''||a[x][y+]=='')
dfs(x,y+,);
else
dfs(x,y+,);
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s %s",a[],a[]);
flag=;
if(a[][]==''||a[][]=='')//只能向右走
dfs(,,);
else//不然只能向下走
dfs(,,);
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return ;
}

CodeForces-1234C-Pipes-dfs的更多相关文章

  1. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  2. codeforces 731C(DFS)

    题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...

  3. codeforces 723D(DFS)

    题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...

  4. Military Problem CodeForces 1006E (dfs序)

    J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...

  5. CodeForces - 95B(DFS)

    题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...

  6. CodeForces - 589J(DFS)

    题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...

  7. CodeForces 1099F - Cookies - [DFS+博弈+线段树]

    题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting gam ...

  8. Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)

    Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...

  9. Codeforces 723d [暴力dfs]

    /* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给n,m和k,n和m为所给矩阵的高和宽.k是要求最多剩下的湖的数量. 在所给的矩阵中,*代表陆地,.代表水. 湖的定义是一片连续的水(上下左右四 ...

  10. CodeForces 378C Maze (DFS)

    题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...

随机推荐

  1. vue中的toast组件

    首先在components新建组件文件夹 随后在toast.vue中写入弹框样式 <template> <transition name="demo"> & ...

  2. JS闭包的详解

    目录 一.什么是闭包? 二.闭包有什么好处?应用在哪? 2.1 好处: 2.2 用法: 三.闭包需要注意的地方? 3.1 IE下会引发内存泄露 一.什么是闭包? 特点: 1 函数嵌套函数 2 内部函数 ...

  3. css 导航样式

    html  结构 <div class="nav-menu float-r"> <ul class="menu-item"> <l ...

  4. Makefile中的函数

    Makefile 中的函数 Makefile 中自带了一些函数, 利用这些函数可以简化 Makefile 的编写. 函数调用语法如下: $(<function> <arguments ...

  5. eclipse修改项目访问前缀

    eclipse项目右击 properties---web project setting---context root修改项目访问前缀

  6. jquery中的ajax方法参数的用法和他的含义

    jquery中的ajax方法参数的用法和他的含义: 1.url:  要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type:  要求为String类型的参数,请求方式(pos ...

  7. Xen的体系结构

    1. BIOS的虚拟化 xen的启动过程,与x86系统一样,首先要进入保护模式,然后安装中断处理程序. xen的中断处理程序可以分为几种,有的是直接分发给正在运行的操作系统,有的是分发给安装了硬件驱动 ...

  8. vue-lic工具搭建vue-webpack项目

    1.安装node环境(安装node时候会自动安装npm) 参考官网:https://nodejs.org/en/download/ 2.安装vue的脚手架工具vue-cli // 全局安装 npm i ...

  9. Feign 系列(05)Spring Cloud OpenFeign 源码解析

    Feign 系列(05)Spring Cloud OpenFeign 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/ ...

  10. [Code+#3]博弈论与概率统计

    题目 记得曾经和稳稳比谁后抄这个题的题解,看来是我输了 不难发现\(p\)是给着玩的,只需要求一个总情况数除以\(\binom{n+m}{n}\)就好了 记\(i\)为无效的失败次数,即\(\rm A ...