-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1365d.cpp
More file actions
88 lines (85 loc) · 2.03 KB
/
1365d.cpp
File metadata and controls
88 lines (85 loc) · 2.03 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
/**
* author: vishnus
* created: 2022-08-27
**/
#include <bits/stdc++.h>
using namespace std;
// Idea: make neighbouring cells of 'B' walls and flood fill. so easy.
int main() {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
vector<pair<int, int>> d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int tt;
cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
vector<vector<char>> a(n, vector<char>(m));
int gcnt = 0, bcnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
gcnt += a[i][j] == 'G';
bcnt += a[i][j] == 'B';
}
}
bool ok = true;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'B') {
for (auto [dr, dc] : d) {
int nr = dr + i;
int nc = dc + j;
if (nr >= 0 && nr < n && nc >= 0 && nc < m && a[nr][nc] != 'B') {
if (a[nr][nc] == 'G') {
ok = false;
break;
}
a[nr][nc] = '#';
if (nr == n - 1 && nc == m - 1 && gcnt >= 1) {
ok = false;
break;
}
}
}
}
}
}
if (!ok) {
cout << "NO" << '\n';
continue;
}
vector<vector<int>> vis(n, vector<int>(m));
queue<pair<int, int>> q;
q.push({n - 1, m - 1});
vis[n - 1][m - 1] = 1;
while (!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
for (auto [dr, dc] : d) {
int nr = dr + r;
int nc = dc + c;
if (nr >= 0 && nr < n && nc >= 0 && nc < m && !vis[nr][nc] && a[nr][nc] != '#') {
q.push({nr, nc});
vis[nr][nc] = 1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'G' && !vis[i][j]) {
ok = false;
break;
}
}
}
if (!ok) {
cout << "NO" << '\n';
continue;
}
cout << "YES" << '\n';
}
}