Educational Codeforces Round 139——C. Hamiltonian Wall

334 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第20天,点击查看活动详情

Educational Codeforces Round 139——C. Hamiltonian Wall

Problem - C - Codeforces

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=ci,j= 'B', and to be left white, if ci,j=ci,j= 'W'. Additionally, he wants each column to have at least one black cell, so, for each jj, the following constraint is satisfied: c1,jc1,j, c2,jc2,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)(x1,y1) and move it along the path (x1,y1),(x2,y2),…,(xk,yk)(x1,y1),(x2,y2),…,(xk,yk) so that:

  • for each ii, (xi,yi)(xi,yi) and (xi+1,yi+1)(xi+1,yi+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.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer mm (1≤m≤2⋅1051≤m≤2⋅105) — the number of columns in the wall.

The ii-th of the next two lines contains a string cici, consisting of mm characters, where each character is either 'B' or 'W'. ci,jci,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,jc1,j, c2,jc2,j or both of them are equal to 'B'.

The sum of mm over all testcases doesn't exceed 2⋅1052⋅105.

Output

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

Example

input

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

output

YES
YES
NO
NO
NO
YES

问题解析

题目是说给两行字符串,只由‘B'和’W'组成,问我们能不能一次走遍所有的B,且不走重复的路线。

先从左往右找第一个B出现的位置,然后以那个位置为起点进行dfs。

如果当前位置的上面or下面有B字符,优先走上面or下面;如果没有,则往右走。

每次dfs只会往一个方向走。

走到不能走为止,把经过的B都变成W,最后再遍历一遍字符串,如果还有B没变成W,就输出NO,反之YES。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>
​
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 3e5 + 50, MOD = 1e9 + 7;
​
int n;
char a[3][N];
void dfs(int x, int y)
{
    a[x][y] = 'W';
    if (x == 1 && a[x + 1][y] == 'B')
        dfs(x + 1, y);
    else if (x == 2 && a[x - 1][y] == 'B')
        dfs(x - 1, y);
    else if (y < n && a[x][y + 1] == 'B')
        dfs(x, y + 1);
}
void solve()
{
    string s1, s2;
    cin >> n >> s1 >> s2;
    int x = -1, y = -1;
    for (int i = 1; i <= n; i++)
    {
        a[1][i] = s1[i-1];
        a[2][i] = s2[i-1];
        if (x == -1 && y == -1)
        {
            if (a[1][i] == 'B')
                x = 1, y = i;
            
        }
    }
    if (x != -1 && y != -1)
    {
        dfs(x, y);
    }
    bool flag = true;
    for(int i=1;i<=2;i++)
        for (int j = 1; j <= n; j++)
        {
            if (a[i][j] == 'B')
            {
                flag = false;
                break;
            }
        }
    if (flag)
    {
        cout << "YES" << endl;
        return;
    }
    x = -1, y = -1;
    for (int i = 1; i <= n; i++)
    {
        a[1][i] = s1[i - 1];
        a[2][i] = s2[i - 1];
        if (x == -1 && y == -1)
        {
            if (a[2][i] == 'B')
                x = 2, y = i;
​
        }
    }
    dfs(x, y);
    for (int i = 1; i <= 2; i++)
        for (int j = 1; j <= n; j++)
        {
            if (a[i][j] == 'B')
            {
                cout << "NO" << endl;
                return;
            }
        }
    cout << "YES" << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
​
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}