Vivek has encountered a problem. He has a maze that can be represented as an n×m grid. Each of the grid cells may represent the following:

Empty — ‘.’
Wall — ‘#’
Good person — ‘G’
Bad person — ‘B’
The only escape from the maze is at cell (n,m).

A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains ‘G’ or ‘B’ cannot be blocked and can be travelled through.

Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.

It is guaranteed that the cell (n,m) is empty. Vivek can also block this cell.

Input

The first line contains one integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, m (1≤n,m≤50) — the number of rows and columns in the maze.

Each of the next n lines contain m characters. They describe the layout of the maze. If a character on a line equals ‘.’, the corresponding cell is empty. If it equals ‘#’, the cell has a wall. ‘G’ corresponds to a good person and ‘B’ corresponds to a bad person.

Output

For each test case, print “Yes” if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print “No”

You may print every letter in any case (upper or lower).

思路:

先判断好人和坏人是否相邻,再把坏人周围的’.‘换成’#’,最后从(m,n)开始走看能否找到所有的好人。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
char a[100][100];
int aa[4] = {0,0,1,-1},bb[4] = {1,-1,0,0},m,n;
int vis[100][100];
struct node{
int x,y;
};
int maxx;
queue<node>q;
int bfs(int sum,int maxx)
{
while(!q.empty())
q.pop();
node now,next;
now.x = m,now.y = n;
q.push(now);
while(!q.empty())
{
now = q.front();
q.pop();
if(a[now.x][now.y] == 'G')
{
maxx++;
}
if(a[now.x][now.y] == '#')
{
if(maxx == sum)
return 1;
}
if(a[now.x][now.y] == 'B')
{
return 0;
}
for(int i = 0; i < 4; i++)
{
next.x = now.x + aa[i];
next.y = now.y + bb[i];
if(next.x >= 1 && next.x <= m && next.y >= 1 && next.y <= n && a[next.x][next.y] != '#' && !vis[next.x][next.y])
{
vis[next.x][next.y] = 1;
q.push(next);
}
}
}
if(sum == maxx)
return 1;
else
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m);

int sum = 0,flag = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
scanf(" %c",&a[j][i]);
if(a[j][i] == 'G')
{
sum++;
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(a[j][i] == 'B')
{
for(int k = 0; k < 4; k++)
{
int x = j+aa[k];
int y = i+bb[k];
if(a[x][y] == 'G')
flag = 1;
}
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(a[j][i] == 'B')
{
if(a[j+1][i] == '.')
a[j+1][i] = '#';
if(a[j-1][i] == '.')
a[j-1][i] = '#';
if(a[j][i+1] == '.')
a[j][i+1] = '#';
if(a[j][i-1] == '.')
a[j][i-1] = '#';
}
}
}
if(bfs(sum,0) && flag == 0)
printf("Yes\n");
else
printf("No\n");

}
}