CF1234C.Pipes

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

你被给定一个管道系统。该系统由两行组成,每行包含 nn 个管道。左上角的管道坐标为 (1,1)(1, 1),右下角的管道坐标为 (2,n)(2, n)。

共有六种类型的管道:两种直管和四种弯管。以下是全部六种类型的示例:

管道类型

你可以对每个给定的管道任意(可能为零次)地顺时针或逆时针旋转 90∘90^\circ(因此类型 11 和 22 可以互相转换,类型 3,4,5,63, 4, 5, 6 之间也可互相转换)。

你的目标是通过旋转若干管道,使得水流能从 (1,0)(1, 0)(即左上角管道左侧)出发,流入位于 (1,1)(1, 1) 的管道,并经由相互连通的管道流动,最终到达位于 (2,n)(2, n) 的管道,再向右流出至 (2,n+1)(2, n + 1)。

当两个管道在系统中相邻,且它们相接的端口彼此连通时,这两个管道即视为连通。以下是连通管道的示例:

连通管道示例

我们用一个例子来描述该问题:

第一个样例输入

其解如下所示:

第一个样例输出

如你所见,水流是一条绘制得较为粗糙的蓝色线条。为得到该解,我们需要将位于 (1,2)(1, 2) 的管道顺时针旋转 90∘90^\circ,将位于 (2,3)(2, 3) 的管道旋转 90∘90^\circ,将位于 (1,6)(1, 6) 的管道旋转 90∘90^\circ,将位于 (1,7)(1, 7) 的管道旋转 180∘180^\circ,并将位于 (2,7)(2, 7) 的管道旋转 180∘180^\circ。此后,水流即可从 (1,0)(1, 0) 流至 (2,n+1)(2, n + 1)。

你需要回答 qq 个相互独立的查询。

输入格式

The first line of the input contains one integer qq (1≤q≤1041 \le q \le 10^4) — 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 \le n \le 2 \cdot 10^5) — 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 \cdot 10^5.

输入的第一行包含一个整数 qq(1≤q≤1041 \le q \le 10^4),表示查询次数。随后是 qq 次查询。

每次查询恰好由三行组成。查询的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示每行中管道的数量。接下来的两行分别描述第一行和第二行的管道布局。每行的描述由 nn 个数字(取值范围为 11 到 66)组成,数字间无空格;每个数字对应相应位置处管道的类型。有关各数字与管道类型之间的对应关系,请参见题目描述。

保证所有查询中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

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.

对于第 ii 个查询,请输出其答案——如果可以通过旋转某些管道,使得水流能够从 (1,0)(1, 0) 到达 (2,n+1)(2, n + 1),则输出 "YES"(不带引号),否则输出 "NO"。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    YES
    YES
    NO
    YES
    NO

说明/提示

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

示例中的第一个查询已在题目描述中给出。

输入解题思路,AI测评打分。不知道怎么写?

首页