forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path( CODEFORCES ) Solve The Maze 1365D.cpp
111 lines (100 loc) · 2.61 KB
/
( CODEFORCES ) Solve The Maze 1365D.cpp
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
109
110
111
// author : hrahul2605
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define int long long
#define f first
#define se second
#define LB lower_bound
#define UB upper_bound
#define pb push_back
#define mp make_pair
#define vll vector<ll>
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define pie 3.14159265358979323846264338327950288
const unsigned int MOD = 1000000007;
using namespace std;
const int maxN = 51;
ll n,m;
char a[maxN][maxN];
ll visited[maxN][maxN];
int DFS(int x, int y)
{
ll ans = 0 ;
visited[x][y]=1;
if(x==n && y==m)
ans = 1;
if(a[x+1][y]!='#' && x+1<=n && !visited[x+1][y])
ans = ans || DFS(x+1,y);
if(a[x][y+1]!='#' && y+1<=m && !visited[x][y+1])
ans = ans || DFS(x,y+1);
if(a[x-1][y]!='#' && x-1>=1 && !visited[x-1][y])
ans = ans || DFS(x-1,y);
if(a[x][y-1]!='#' && y-1>=1 && !visited[x][y-1])
ans = ans || DFS(x,y-1);
return ans ;
}
int32_t main()
{
IOS;
int T;
cin>>T;
// T=1;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i = 1; i<=n;i++)
{
for(int j = 1 ; j<=m ; j++)
{
if(a[i][j]=='B')
{
if(j+1<=m && a[i][j+1]=='.')
a[i][j+1] = '#';
if(j-1>0 && a[i][j-1]=='.')
a[i][j-1] = '#';
if(i+1<=n && a[i+1][j]=='.')
a[i+1][j] = '#';
if(i-1>0 && a[i-1][j]=='.')
a[i-1][j] = '#';
}
}
}
ll ans = 1;
for(int i=1;i<=n;i++){
for(int j=1; j<=m;j++)
{
if(a[i][j]=='G'){
memset(visited,0,sizeof(visited));
ans*=DFS(i,j);
}
}
}
for(int i =1; i<=n ; i++)
{
for(int j =1; j<=m ; j++){
if(a[i][j]=='B')
{
if(i+1<=n && a[i+1][j]=='G')
ans = 0;
if(i-1>0 && a[i-1][j]=='G')
ans = 0;
if(j+1<=m && a[i][j+1]=='G')
ans = 0;
if(j-1>0 && a[i][j-1]=='G')
ans = 0;
}
}
}
if(ans)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}