原题链接:https://vjudge.net/contest/331120#problem/E

原文英语:

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) 180180 degrees. 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 nn digits 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.

Example

Input
6         //6组数据
7 //2*7
2323216
1615124
1 //2*1
3
4
2 //2*2
13
24
2 //2*2
12
34
3 //2*3
536
345
2 //2*2
46
54
Output
YES
YES
YES
NO
YES
NO

Note

The first query from the example is described in the problem statement.

代码:

#include<stdio.h>
#include<string.h>
int flag,n;
char a[2][200020];    //1左、2下、3右、4上
void dfs(int x,int y,int ss) { //从a[x][y]流过来,方向ss
 if(y>=n)
  return;
 if(x==1&&y==n-1&&ss==3) { //x、y表示已经走到右下角了,ss=3表示是向右流出去的,成立
  flag=1;
  return;
 }
 if(x==0) { //此时水流第一行,判断此时水的方向,如果再第一行,方向只能向下或向右
  if(ss==2) { // 方向为向下
   if(a[x+1][y]!='1'&&a[x+1][y]!='2')
    dfs(x+1,y,3);
  } else if(ss==3) { //方向为向右
   if(a[x][y+1]=='1'||a[x][y+1]=='2')
    dfs(x,y+1,3);
   else dfs(x,y+1,2);
  }
 } else if(x==1) { //从下一行流过来的,方向只能是向上或者向右
  if(ss==4) { //上
   if(a[x-1][y]!='1'&&a[x-1][y]!='2')
    dfs(x-1,y,3);
  } else if(ss==3) { //右
   if(a[x][y+1]=='1'||a[x][y+1]=='2')
    dfs(x,y+1,3);
   else
    dfs(x,y+1,4);
  }
 }
}
int main() {
 int t;
 scanf("%d",&t);
 while(t--) {
  scanf("%d",&n);
  scanf("%s %s",a[0],a[1]);  //现在定义了一个二位数组a[2][12], 然后 scanf("%s %s",a[0],a[1]),分别是输入到a的第一行和第二行
  flag=0;
  if(a[0][0]=='1'||a[0][0]=='2')//   判断起点管子  成立时只能向右走
   dfs(0,0,3);
  else       //不然只能向下走
   dfs(0,0,2);
  if(flag)
   printf("YES\n");
  else
   printf("NO\n");
 }
 return 0;
}
 
 
 
 

题意:

给出t组数据,每组数据给出两组长度为n的字符串,拼接起来代表一个长为n宽为2的长方形,水(0,0)进入,从右下角那个格子横着流出。若能从开头流出去,输出YES,否则输出NO。

水管有以下几种类型,可以90度旋转任意次,因为可以通过旋转得到,所以1、2可以看作是A类型,3-6可以看作是B类型。

思路:

首先判断起点是什么类型的水管,进行dfs,自己定义四个方向(上下左右),开始进行dfs。dfs(x,y,ss),x、y代表传入的下标,表示当前走到的点,ss表示当前的流向,然后再对当前流向所能到达的那个点

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

 

cf 水管问题的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. while 循环 实例

    /*int i=0; while(i<100){// 循环条件 while先执行后循环 printf("while第%d遍循环体\n",i);//循环体 i++; } */ ...

  2. HTML连载71-翻转菜单练习

    一.翻转菜单练习​ <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. MyEclipse10下载安装破解及汉化内含jdk8u241及其帮助文档

    下载MyEclipse10以及破解包 MyEclipse10: 提取码:020c 破解包 提取码:mycj 注:破解包内含有破解教程,很详细,这里就不多说了 MyEclipse10汉化 操作系统:wi ...

  4. 伪造TGT黄金票据

    通过上一篇文章我们初步了解了Kerberos协议的工作过程,解决的两个问题 第一个问题:如何证明你本人是XXX用户的问题   由Authentication Server负责 第二个问题:提供服务的服 ...

  5. 一行代码解决MacBook Pro安装VSCode没有应用图标问题

    笔者今天升级了VSCode,安装完后发现Dock(程序坞)没有VSCode的图标了,导致切换应用非常不方便. 具体情况就像下面这张图,VSCode明明开着,但是在Dock找不到VSCode了. 解决办 ...

  6. You (oracle) are not allowed to use this program (crontab)

      检查一台ORACLE数据库服务器的crontab作业(用户为oracle,实际环境中可能为oracle.也有可能是其它用户)时,发现出现下面提示信息: $ crontab -l You (orac ...

  7. docker之阿里云centos 7.x 启动容器报错处理办法

    最近阿里云服务器(操作系统centOS 7.x) 安装docker,参照阿里云帮助文档https://help.aliyun.com/document_detail/51853.html?spm=a2 ...

  8. Linux.CentOS下载

    1.[CentOS]centos7 稳定使用版本,centos镜像的下载 - Angel挤一挤 - 博客园.html(https://www.cnblogs.com/sxdcgaq8080/p/106 ...

  9. 前端开发神器 VSCode 使用总结

    VSCode 是微软出品的,基于 Electron 和 TypeScript 的,集成了 git 版本管理和命令行终端,而且开源稳定,插件丰富,再搭配一款 Chrome 浏览器,可以说是前端开发神器了 ...

  10. 《趣谈 Linux 操作系统》学习笔记(一):为什么要学 Linux 及学习路径

    前言:学习的课程来自极客时间的专栏<趣谈 Linux 操作系统>,作者用形象化的比喻和丰富的图片让课程变得比较易懂,为了避免知识看过就忘,打算通过写学习笔记的形式记录自己的学习过程. Li ...