Repository navigation
Expand file tree
/
Copy pathD.cpp
More file actions
108 lines (107 loc) · 2.29 KB
/
Copy pathD.cpp
File metadata and controls
108 lines (107 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
© 2020-06-07 09:35:03 Franco1010 All Rights Reserved
*/
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int,int> ii;
#define deb(x) cout << #x": " << (x) << endl;
const int N = 55;
lli n, m;
string g[N];
const int cx[4] = {0, 0, 1, -1};
const int cy[4] = {1, -1, 0, 0};
bool valid(int x, int y){
return 0 <= x and x < n and 0 <= y and y < m;
}
bool block(int x, int y){
bool yes = true;
g[x][y] = '#';
fore(i, 0, 4){
int nx = x + cx[i];
int ny = y + cy[i];
if(valid(nx, ny)){
if(g[nx][ny] == 'G') return false;
else if(g[nx][ny] == 'B') yes &= block(nx, ny);
else g[nx][ny] = '#';
}
}
return yes;
}
int vis[N][N];
int curvis;
int bfs(set<ii> st){
lli res = 0;
curvis++;
queue<ii> q;
q.push({n - 1, m - 1});
vis[n - 1][m - 1] = curvis;
while(!q.empty()){
auto u = q.front(); q.pop();
if(st.count(u)) res++;
fore(i, 0, 4){
int nx = u.f + cx[i];
int ny = u.s + cy[i];
if(valid(nx, ny) and g[nx][ny] != '#' and vis[nx][ny] != curvis){
vis[nx][ny] = curvis;
q.push({nx, ny});
}
}
}
return res;
}
void solve(){
cin >> n >> m;
bool somebody = false;
// bool good = false;
set<ii> v;
fore(i, 0, n){
cin >> g[i];
fore(j, 0, m){
if(g[i][j] == 'B' or g[i][j] == 'G'){
somebody = true;
if(g[i][j] == 'G') v.insert({i, j});
}
}
}
if(!somebody or v.empty()) {
cout << "Yes" << ENDL;
return;
}
else{
fore(i, 0, n){
fore(j, 0, m){
if(g[i][j] == 'B'){
if(!block(i, j)){
cout << "No" << ENDL;
return;
}
}
}
}
}
// fore(i, 0, n) cout << g[i] << ENDL;
// bool yes = true;
// for(auto i : v) if(!bfs(i)){
// cout << "No" << ENDL;
// return;
// }
if((g[n - 1][m - 1] == '.' or g[n - 1][m - 1] == 'G') and bfs(v) == sz(v))cout << "Yes" << ENDL;
else cout << "No" << ENDL;
}
int main(){ _
lli t; cin >> t;
while(t--){
solve();
}
return 0;
}