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. PHP FILTER_VALIDATE_BOOLEAN 过滤器

    定义和用法 FILTER_VALIDATE_BOOLEAN 过滤器把值作为布尔选项来验证. Name: "boolean" ID-number: 258 可能的返回值: 如果是 & ...

  2. Linux服务器的16个监控命令

    想不想知道你的服务器到底在干什么?那么你要知道本文介绍的这些基本命令.一旦你熟悉掌握了这些命令,就为成为专业的 Linux系统管理员打下了基础. 你可以通过图形化用户界面(GUI)程序来获取这些外壳命 ...

  3. NX二次开发-Block UI C++界面关于 在Block UI中UF_initialize();和UF_terminate();的使用

    关于 在Block UI中UF_initialize();和UF_terminate();的使用 用Block UI作NX二次开发的时候,不需要在使用UFUN函数的时候加UF_initialize() ...

  4. 20165239 2018——2019Exp8 Web基础

    Exp8 Web基础 基础问题回答 (1)什么是表单 •表单在网页中主要负责数据采集功能. •一个表单有三个基本组成部分: ◦表单标签,这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务 ...

  5. 火狐RESTClient和HttpRequester, Chrome的Postman使用详解

    Chrome下有著名的Postman,那火狐也有它的左膀右臂,那就是RESTClient和HttpRequester.这两款工具都是火狐的插件,主要用来模拟发送HTTP请求,HTTP请求最常用的两种方 ...

  6. 1251 client does not support

    1.mysql -uroot -p 123456        (用户root,密码123465) 2.use mysql: 3.ALTER USER 'root'@'localhost' IDENT ...

  7. Linux 一些常识命令

    linux的性能优化: .CPU,MEM .DISK--RAID .网络相关的外设,网卡 linux系统性能分析: top:linux系统的负载,CPU,MEM,SWAP,占用CPU和内存比较的进程, ...

  8. JUC源码分析-集合篇(一)ConcurrentHashMap

    JUC源码分析-集合篇(一)ConcurrentHashMap 1. 概述 <HashMap 源码详细分析(JDK1.8)>:https://segmentfault.com/a/1190 ...

  9. Cyclical Quest CodeForces - 235C 后缀自动机

    题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...

  10. springfox-swagger原理解析与使用过程中遇到的坑

    swagger简介 swagger确实是个好东西,可以跟据业务代码自动生成相关的api接口文档,尤其用于restful风格中的项目,开发人员几乎可以不用专门去维护rest api,这个框架可以自动为你 ...