-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path095-solve-the-maze.cpp
More file actions
105 lines (102 loc) · 2.7 KB
/
Copy path095-solve-the-maze.cpp
File metadata and controls
105 lines (102 loc) · 2.7 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
/*
* Solve The Maze [1365D]
* Problem: https://codeforces.com/problemset/problem/1365/D
* Verdict: ACCEPTED Solved: 2022-01-13
* Language: C++20 (GCC 11-64)
* Runtime: 31 ms Memory: 400 KB
* Tags: constructive algorithms, dfs and similar, dsu, graphs, greedy, implementation, shortest paths
* Author: BidoTeima
* Source: https://codeforces.com/contest/1365/submission/142650663
*/
/// isA AC
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void ACPLS()
{
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
#define tc \
int tttttt,subtask; \
cin >> tttttt /*>> subtask*/; \
while (tttttt--)
#define sumrange(l, r, arr) (l == 0 ? arr[r] : arr[r] - arr[l - 1])
#define all(v) v.begin(), v.end()
char grid[55][55];
bool vis[55][55];
vector<pair<int,int>>adj[55][55];
int n,m;
bool valid(int i, int j){
return(1<=i&&i<=n&&1<=j&&j<=m&&grid[i][j]!='#')&&vis[i][j]==0;
}
void dfs(int i, int j){
vis[i][j]=1;
for(auto i:adj[i][j])
if(!vis[i.first][i.second])
dfs(i.first,i.second);
}
int main()
{
ACPLS();
tc{
memset(vis,0,sizeof(vis));
cin>>n>>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
adj[i][j].clear();
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin>>grid[i][j];
}
}
bool ans=1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(grid[i][j]=='B'){
if(valid(i-1,j)&&grid[i-1][j]=='.'){
grid[i-1][j]='#';
}
if(valid(i,j-1)&&grid[i][j-1]=='.'){
grid[i][j-1]='#';
}
if(valid(i+1,j)&&grid[i+1][j]=='.'){
grid[i+1][j]='#';
}
if(valid(i,j+1)&&grid[i][j+1]=='.'){
grid[i][j+1]='#';
}
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(grid[i][j]!='#'){
if(valid(i-1,j)){adj[i][j].push_back({i-1,j});}
if(valid(i,j-1)){adj[i][j].push_back({i,j-1});}
if(valid(i+1,j)){adj[i][j].push_back({i+1,j});}
if(valid(i,j+1)){adj[i][j].push_back({i,j+1});}
}
}
}
dfs(n,m);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(grid[i][j]=='G'){
//cout<<i<<' '<<j<<": "<<vis[i][j]<<'\n';
ans&=vis[i][j];
}
if(grid[i][j]=='B')
ans&=!vis[i][j];
}
}
cout<<(ans?"Yes\n":"No\n");
}
}