CF1766C.Hamiltonian Wall

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Sir Monocarp Hamilton is planning to paint his wall. The wall can be represented as a grid, consisting of 22 rows and mm columns. Initially, the wall is completely white.

Monocarp wants to paint a black picture on the wall. In particular, he wants cell (i,j)(i, j) (the jj-th cell in the ii-th row) to be colored black, if ci,j=c_{i, j} = 'B', and to be left white, if ci,j=c_{i, j} = 'W'. Additionally, he wants each column to have at least one black cell, so, for each jj, the following constraint is satisfied: c1,jc_{1, j}, c2,jc_{2, j} or both of them will be equal to 'B'.

In order for the picture to turn out smooth, Monocarp wants to place down a paint brush in some cell (x1,y1)(x_1, y_1) and move it along the path (x1,y1),(x2,y2),…,(xk,yk)(x_1, y_1), (x_2, y_2), \dots, (x_k, y_k) so that:

  • for each ii, (xi,yi)(x_i, y_i) and (xi+1,yi+1)(x_{i+1}, y_{i+1}) share a common side;
  • all black cells appear in the path exactly once;
  • white cells don't appear in the path.

Determine if Monocarp can paint the wall.

单胞君哈密顿正计划粉刷他的墙壁。这面墙可以表示为一个 22 行 mm 列的网格,初始状态下整面墙均为白色。

单胞君希望在墙上绘制一幅黑色图案。具体而言,他希望单元格 (i,j)(i, j)(即第 ii 行第 jj 列的单元格)被涂成黑色当且仅当 ci,j=c_{i, j} = 'B';若 ci,j=c_{i, j} = 'W',则保持白色。此外,他要求每一列至少有一个黑色单元格,即对每个 jj,以下约束成立:c1,jc_{1, j}、c2,jc_{2, j} 中至少有一个等于 'B'。

为了使图案绘制得平滑流畅,单胞君希望将画笔起始于某个单元格 (x1,y1)(x_1, y_1),并沿路径 (x1,y1),(x2,y2),…,(xk,yk)(x_1, y_1), (x_2, y_2), \dots, (x_k, y_k) 移动,使得:

  • 对每个 ii,(xi,yi)(x_i, y_i) 与 (xi+1,yi+1)(x_{i+1}, y_{i+1}) 共享一条边(即相邻);
  • 所有黑色单元格在该路径中恰好出现一次;
  • 白色单元格不出现在该路径中。

请判断单胞君能否成功绘制出这幅墙画。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer mm (1≤m≤2⋅1051 \le m \le 2 \cdot 10^5) — the number of columns in the wall.

The ii-th of the next two lines contains a string cic_i, consisting of mm characters, where each character is either 'B' or 'W'. ci,jc_{i, j} is 'B', if the cell (i,j)(i, j) should be colored black, and 'W', if the cell (i,j)(i, j) should be left white.

Additionally, for each jj, the following constraint is satisfied: c1,jc_{1, j}, c2,jc_{2, j} or both of them are equal to 'B'.

The sum of mm over all testcases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。

每个测试用例的第一行包含一个整数 mm(1≤m≤2⋅1051 \le m \le 2 \cdot 10^5)——墙的列数。

接下来两行中的第 ii 行包含一个长度为 mm 的字符串 cic_i,其中每个字符为 'B' 或 'W'。若单元格 (i,j)(i, j) 应涂成黑色,则 ci,jc_{i, j} 为 'B';若应保持白色,则为 'W'。

此外,对每个 jj,以下约束成立:c1,jc_{1, j}、c2,jc_{2, j} 中至少有一个等于 'B'。

所有测试用例的 mm 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each testcase, print "YES" if Monocarp can paint a wall. Otherwise, print "NO".

对于每个测试用例,如果 Monocarp 能够粉刷一堵墙,则输出 "YES";否则输出 "NO"。

输入输出样例

  • 输入#1

    6
    3
    WBB
    BBW
    1
    B
    B
    5
    BWBWB
    BBBBB
    2
    BW
    WB
    5
    BBBBW
    BWBBB
    6
    BWBBWB
    BBBBBB

    输出#1

    YES
    YES
    NO
    NO
    NO
    YES

说明/提示

In the first testcase, Monocarp can follow a path (2,1)(2, 1), (2,2)(2, 2), (1,2)(1, 2), (1,3)(1, 3) with his brush. All black cells appear in the path exactly once, no white cells appear in the path.

In the second testcase, Monocarp can follow a path (1,1)(1, 1), (2,1)(2, 1).

In the third testcase:

  • the path (1,1)(1, 1), (2,1)(2, 1), (2,2)(2, 2), (2,3)(2, 3), (1,3)(1, 3), (2,4)(2, 4), (2,5)(2, 5), (1,5)(1, 5) doesn't suffice because a pair of cells (1,3)(1, 3) and (2,4)(2, 4) doesn't share a common side;
  • the path (1,1)(1, 1), (2,1)(2, 1), (2,2)(2, 2), (2,3)(2, 3), (1,3)(1, 3), (2,3)(2, 3), (2,4)(2, 4), (2,5)(2, 5), (1,5)(1, 5) doesn't suffice because cell (2,3)(2, 3) is visited twice;
  • the path (1,1)(1, 1), (2,1)(2, 1), (2,2)(2, 2), (2,3)(2, 3), (2,4)(2, 4), (2,5)(2, 5), (1,5)(1, 5) doesn't suffice because a black cell (1,3)(1, 3) doesn't appear in the path;
  • the path (1,1)(1, 1), (2,1)(2, 1), (2,2)(2, 2), (2,3)(2, 3), (2,4)(2, 4), (2,5)(2, 5), (1,5)(1, 5), (1,4)(1, 4), (1,3)(1, 3) doesn't suffice because a white cell (1,4)(1, 4) appears in the path.

在第一个测试用例中,Monocarp 可以用画笔沿路径 (2,1)(2, 1)、(2,2)(2, 2)、(1,2)(1, 2)、(1,3)(1, 3) 行进。所有黑色格子在该路径中恰好出现一次,且路径中不包含任何白色格子。

在第二个测试用例中,Monocarp 可以沿路径 (1,1)(1, 1)、(2,1)(2, 1) 行进。

在第三个测试用例中:

  • 路径 (1,1)(1, 1)、(2,1)(2, 1)、(2,2)(2, 2)、(2,3)(2, 3)、(1,3)(1, 3)、(2,4)(2, 4)、(2,5)(2, 5)、(1,5)(1, 5) 不满足要求,因为格子对 (1,3)(1, 3) 和 (2,4)(2, 4) 不共享一条公共边;
  • 路径 (1,1)(1, 1)、(2,1)(2, 1)、(2,2)(2, 2)、(2,3)(2, 3)、(1,3)(1, 3)、(2,3)(2, 3)、(2,4)(2, 4)、(2,5)(2, 5)、(1,5)(1, 5) 不满足要求,因为格子 (2,3)(2, 3) 被访问了两次;
  • 路径 (1,1)(1, 1)、(2,1)(2, 1)、(2,2)(2, 2)、(2,3)(2, 3)、(2,4)(2, 4)、(2,5)(2, 5)、(1,5)(1, 5) 不满足要求,因为黑色格子 (1,3)(1, 3) 未出现在该路径中;
  • 路径 (1,1)(1, 1)、(2,1)(2, 1)、(2,2)(2, 2)、(2,3)(2, 3)、(2,4)(2, 4)、(2,5)(2, 5)、(1,5)(1, 5)、(1,4)(1, 4)、(1,3)(1, 3) 不满足要求,因为白色格子 (1,4)(1, 4) 出现在该路径中。

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

首页