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).
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.
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.
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.
6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54
YES
YES
YES
NO
YES
NO
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的更多相关文章
- codeforces 734E(DFS,树的直径(最长路))
题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- codeforces 723D(DFS)
题目链接:http://codeforces.com/problemset/problem/723/D 题意:n*m的矩阵中,'*'代表陆地,'.'代表水,连在一起且不沿海的水形成湖泊.问最少填多少块 ...
- Military Problem CodeForces 1006E (dfs序)
J - Military Problem CodeForces - 1006E 就是一道dfs序的问题 给定一个树, 然后有q次询问. 每次给出u,k, 求以u为根的子树经过深搜的第k个儿子,如果一个 ...
- CodeForces - 95B(DFS)
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...
- CodeForces - 589J(DFS)
题目链接:http://codeforces.com/problemset/problem/589/J 题目大意:一个机器人打扫一个密闭的房间,房间由一个矩形构成,'*'表示家具,'.'表示该位置为空 ...
- CodeForces 1099F - Cookies - [DFS+博弈+线段树]
题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting gam ...
- Persistent Bookcase CodeForces - 707D (dfs 离线处理有根树模型的问题&&Bitset)
Persistent Bookcase CodeForces - 707D time limit per test 2 seconds memory limit per test 512 megaby ...
- Codeforces 723d [暴力dfs]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给n,m和k,n和m为所给矩阵的高和宽.k是要求最多剩下的湖的数量. 在所给的矩阵中,*代表陆地,.代表水. 湖的定义是一片连续的水(上下左右四 ...
- CodeForces 378C Maze (DFS)
题目链接 题意:给一个由“.”组成的联通区域,求再添加k个‘#'以后还是联通区域的方案. 分析:做题的时候犯二了,用DFS,一直搜到边缘,然后从边缘依次往回 回溯,回溯的过程中填充’#‘ 一直填充k个 ...
随机推荐
- Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class
ylbtech-Java-Class-C:com.ylbtech.api.platfrom.util.RedisUtils.class 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶 ...
- 【HTML】框架集(Framesets)
1.Frameset的使用 所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只 要 <FRAMESET> <FRAME> 即可,而所有框架标记 要放在一个总起的 htm ...
- 记一次Tomcat运行失败记录
记一次Tomcat运行失败记录 如图tomcat运行之后会出现这样的情况,在网上百度之后大部分都说的是web.xml或者其他配置文件的问题,但是根据网上修改了之后却还是老样子. 这里有比较好的网址可以 ...
- Dubbo入门到精通学习笔记(十五):Redis集群的安装(Redis3+CentOS)、Redis集群的高可用测试(含Jedis客户端的使用)、Redis集群的扩展测试
文章目录 Redis集群的安装(Redis3+CentOS) 参考文档 Redis 集群介绍.特性.规范等(可看提供的参考文档+视频解说) Redis 集群的安装(Redis3.0.3 + CentO ...
- Bochs调试加载符号文件的问题
1. Bochs中的调试命令ldsym没有触发的情况. 参考:http://www.ibm.com/developerworks/cn/linux/sdk/lex/ Lex 代表 Lexical An ...
- 1060 Are They Equal (25 分)
1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 1230 ...
- shell 检查文件夹是否包含文件,或者只是空文件
empty_dir_check(){ check_dir=$ if [ -d $check_dir ];then file_list=` -maxdepth -type f` if [ $file_l ...
- Java 设计模式之 装饰者模式
装饰者模式(Decorator Pattern): 概述:装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象 特点: (1) ...
- 使用VBA达到vlookup效果
Function Desc(ProdNum) Desc = Application.WorksheetFunction.VLookup(ProdNum, Range("myTable&quo ...
- 制作 U 盘启动盘
制作 U 盘启动盘 这篇文章说的是 U 盘启动盘是如何运作的,同时有一个既能装 Windows 又能装 Linux 的 U 盘启动盘的例子. U 盘启动盘 当按下开机键后,电脑能启动我原本安装的操作系 ...