Codeforces Round #590 (Div. 3) C——1234C Pipes

D. Distinct Characters Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Example

input

Copy

6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54

output

Copy

YES
YES
YES
NO
YES
NO

Note

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

题目大意:给两行可以旋转的水管,给了六个原型,但是通过旋转可以互相转换,所以实际上只有两种水管,然后判断能不能从左上角走到右上角,同时保证右下角的水管是对着右边的(仔细读题,我就被这个坑惨了)

这个有点像一个4399的小游戏,刚看完题有点懵,想了想发现题目看上去有点吓人,但是其实是个纸老虎,因为水管的流向没什么操作的空间,因为题目要求是从左上角到右下角,那么就不用考虑往左的情况的了,竖着的水管也是不用考虑的,这样就只有2,3,4,6是要考虑的,把每种情况都讨论一下,dfs走的时候记录一下上一根管子的流向结合当前管道的横纵坐标就可以判断当前是哪种管道从而继续dfs了。

代码如下:

#include<bits/stdc++.h>
#include<vector>
#include<map>
#include<queue>
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
char s[4][200005];
int vis[4][200005];
int q, n, flag;
void dfs(int x, int y, int t)                //x,y代表横纵坐标,t代表上一根管道的流向{
     if(x == 2 && y == n && t == 4)           //判断条件
     {
         flag = 1;
         return;
     }
     if(y > (n+5))                            //过界时return
         return;
     if(s[x][y] >= ‘3‘)
     {
         if(t == 0 || (t == 4 && x == 1))     //此时为4号管道
         {
             //printf("1 lujin\n");
             dfs(x+1, y, 2);
         }
         else if(t == 1 && x == 1)            //此时为3号管道
         {
             //printf("2 lujin\n");
             dfs(x, y+1, 4);
         }
         else if(x == 2 && t == 4)            //此时为5号管道
         {
           //printf("3 lujin\n");
             dfs(x-1, y, 1);
         }
         else if(t == 2 && x == 2)            //此时为六号管道
         {
             //printf("4 lujin\n");
             dfs(x, y+1, 4);
         }
     }
     else
     {
         if(t == 4 || t == 0)                  //横竖管道只能转成横管道向右走
         {
             //printf("6 lujin\n");
             dfs(x, y+1, 4);
         }
         else
         return;
     }
}
int main()
{
     scanf("%d", &q);
     while(q--)
     {
         flag = 0;
         scanf("%d", &n);
         for(int i = 1;i <= 2;i++)
         cin>>s[i];
         dfs(1, 0, 0);              //从左上角开始dfs
         if(flag)
             printf("YES\n");
         else
             printf("NO\n");

     }
     return 0;
}

C. PipesYou are given a system of pipes. It consists of two rows, each row consists of n pipes. The top left pipe has the coordinates (1,1) and the bottom right — (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:

You can turn each of the given pipes 90 degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types 1 and 2 can become each other and types 3,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) (to the left of the top left pipe), move to the pipe at (1,1), flow somehow by connected pipes to the pipe at (2,n) and flow right to (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 pipesLet’s describe the problem using some example:
The first example input

And its solution is below:

The first example answerAs 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) 90 degrees clockwise, the pipe at (2,3) 90 degrees, the pipe at (1,6) 90 degrees, the pipe at (1,7) 180 degrees and the pipe at (2,7) 180 degrees. Then the flow of water can reach (2,n+1) from (1,0).
You have to answer q independent queries.
InputThe first line of the input contains one integer q (1≤q≤104) — the number of queries. Then q queries follow.
Each query consists of exactly three lines. The first line of the query contains one integer n (1≤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 n digits from 1 to 6 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 n over all queries does not exceed 2⋅105.
OutputFor the i-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) from (1,0), and “NO” otherwise.————————————————版权声明:本文为CSDN博主「Herod_」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/qq_43461168/article/details/101981692

原文地址:https://www.cnblogs.com/Mamba0Z/p/11632051.html

时间: 2024-08-29 17:21:55

Codeforces Round #590 (Div. 3) C——1234C Pipes的相关文章

Codeforces Round #590 (Div. 3) Editorial

Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同的价格,找出一个最小的可能值price,使得price * n >= sum,sum指的是原来商品价格的总和. 知识点:模拟 思路:求出sum/n向上取整即可,有两种方法.一是使用ceil()函数,但注意ceil()返回的是向上取整后的浮点数,所以要进行强制类型转换:二是直接向下取整,然后用if语句

Codeforces Round #590 (Div. 3)(e、f待补

https://codeforces.com/contest/1234/problem/A A. Equalize Prices Again 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int main(){ 5 int n,a; 6 int t; 7 cin>>t; 8 ll sum = 0,ans; 9 while(t--){ 10 cin>>n;sum = 0

Codeforces Round #590 (Div. 3) C. Pipes

链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists of two rows, each row consists of n pipes. The top left pipe has the coordinates (1,1) and the bottom right - (2,n). There are six types of pipes: two

Codeforces Round #590 (Div. 3)

D. Distinct Characters Queries Description You are given a string ss consisting of lowercase Latin letters and qq queries for this string. Recall that the substring s[l;r]s[l;r] of the string ss is the string slsl+1…srslsl+1…sr. For example, the subs

Codeforces Round #590 (Div. 3) B2. Social Network (hard version)

链接: https://codeforces.com/contest/1234/problem/B2 题意: The only difference between easy and hard versions are constraints on n and k. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l;r] of the string s is the string slsl+1-sr. For example, the substrings

Codeforces Round #590 (Div. 3) F

传送门 题意: 给出一个只含前\(20\)个字符的字符串,现在可以选择一段区间进行翻转,问区间中字符各不相同时,最长长度为多少. 思路: 首先,容易将题意转换为选择两个字符各不相同的区间,然后长度相加取最大: 注意到字符串中满足条件的区间长度不超过\(20*n\),那么处理出所有区间,现在任务即为找到两个区间,其字符各不想同,且长度和最大: 因为最多\(20\)个字符,将满足条件的区间转换为二进制数,任务转换为找到两个数\(a_i,a_j\)满足\(a_i\&a_j=0\)且二进制为\(1\)的

Codeforces Round #590 (Div. 3)补题

要想上2000分,先刷几百道2000+的题再说 ---某神 题目 E F 赛时是否尝试 × × tag math bitmask 难度 2000 2400 状态 ? √ 解 E 待定 F 传送门 第一次接触状态压缩dp的题.这道题转换问题的思路非常巧妙. 原问题: 已知: 一个字符串,可进行不超过一次操作 操作限定: 选择某个子串,使其在原串中翻转 目的:使原串的特征值最大 串的特征值:串任意没有重复字符的子串,其包含字符的种类数 问题的转换: 首先选定一个子串a,之后再找到另一个子串b,使得a

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i