C. Hamiltonian Wall

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

首先,起点一定是在两边,因为不能重复走,从中间开始走只能先一个方向。

规定从左边开始走(从左或者右都是一样的),如果起点所处列两个都是B,那么分别枚举两个作为起点,在一步一步走的过程中,有当前列的B先走当前列的,再向右走,如果遇到W,就是NO,可以用一个标记表示下一步要走到右边的第一行还是第二行

#include<iostream>
#include<cstring>

using namespace std;

const int N = 2 * 200010;

int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		int m;
		cin >> m;
		string a, b;
		cin >> a >> b;
		bool flag1 = false, flag2 = false;
		int st;
		for(int i = 0; a[i]; i ++)
		{
			if(a[i] == 'B' && b[i] == 'B')
			{
				st = i;
				flag1 = true;
				flag2 = true;
				break;
			}
			else if(a[i] == 'B')
			{
				st = i;
				flag1 = true;
				break;
			}
			else if(b[i] == 'B')
			{
				st = i;
				flag2 = true;
				break;
			}
		}
		bool ans = true;
		int sign = 1;
		if(flag1 && flag2)
		{
			//先从上往下走 
			sign = 2;
			for(int i = st; i < m - 1; i ++)
			{
				if(sign == 2)
				{
					if(b[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(a[i + 1] == 'B') 
						sign = 1;
					else 
						sign = 2;
				}
				else
				{
					if(a[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(b[i + 1] == 'B')
						sign = 2;
					else
						sign = 1;
				}
			}
			if(ans)
			cout << "YES\n";
			else
			{
				ans = true;
				sign = 1;
				for(int i = st; i < m - 1; i ++)
				{
					if(sign == 2)
					{
						if(b[i + 1] != 'B')
						{
							ans = false;
							break;
						}
						else if(a[i + 1] == 'B') 
							sign = 1;
						else 
							sign = 2;
					}
					else
					{
						if(a[i + 1] != 'B')
						{
							ans = false;
							break;
						}
						else if(b[i + 1] == 'B')
							sign = 2;
						else
							sign = 1;
					}
				}
				if(ans)
				cout << "YES\n";
				else
				cout << "NO" << endl; 
			}
		}
		else if(flag1)
		{
			sign = 1;
			for(int i = st; i < m - 1; i ++)
			{
				if(sign == 2)
				{
					if(b[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(a[i + 1] == 'B') 
						sign = 1;
					else 
						sign = 2;
				}
				else
				{
					if(a[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(b[i + 1] == 'B')
						sign = 2;
					else
						sign = 1;
				}
			}
			if(ans)
			cout << "YES\n";
			else
			cout << "NO" << endl; 
		}
		else
		{
			sign = 2;
			for(int i = st; i < m - 1; i ++)
			{
				if(sign == 2)
				{
					if(b[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(a[i + 1] == 'B') 
						sign = 1;
					else 
						sign = 2;
				}
				else
				{
					if(a[i + 1] != 'B')
					{
						ans = false;
						break;
					}
					else if(b[i + 1] == 'B')
						sign = 2;
					else
						sign = 1;
				}
			}
			if(ans)
			cout << "YES\n";
			else
			cout << "NO" << endl; 
		}
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值