D. Solve The Maze 【图论】1700分
D. Solve The Maze
Vivek has encountered a problem. He has a maze that can be represented as an 𝑛×𝑚 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 (𝑛,𝑚).
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 (𝑛,𝑚) is empty. Vivek can also block this cell.
Input
The first line contains one integer 𝑡 (1≤𝑡≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers 𝑛, 𝑚 (1≤𝑛,𝑚≤50) — the number of rows and columns in the maze.
Each of the next 𝑛 lines contain 𝑚 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).
Example
input
6 1 1 . 1 2 G. 2 2 #B G. 2 3 G.# B#. 3 3 #B. #.. GG. 2 2 #B B.
output
Yes Yes No No Yes Yes
Note
For the first and second test cases, all conditions are already satisfied.
For the third test case, there is only one empty cell (2,2), and if it is replaced with a wall then the good person at (1,2) will not be able to escape.
For the fourth test case, the good person at (1,1) cannot escape.
For the fifth test case, Vivek can block the cells (2,3) and (2,2).
For the last test case, Vivek can block the destination cell (2,2).
题目大意
给定一个NxM的矩阵,里面有好人,坏人,墙,空格子,现在需要在空格子里面放大于等于0个墙,使得好人都可以到达点(N,M),而坏人不可以。问是否可行。
解法
对于一个坏人,用墙将其围起来,如果围坏人同时也把好人围起来了,那么好人就出不去。所以我们在围坏人都时候,圈要尽量的小,给好人预留更多的空间走向点(N,M)。
还有一点,如果好人和坏人紧挨着,那么就别办法将这个坏人围起来的同时,好人不被围住。
如图,如果围起来的圈太大,那么圈里面的空格子就不能被好人使用了。所以要围起来就一定要最小范围的围。
#include <bits/stdc++.h> #define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug_in freopen("in.txt","r",stdin) #define debug_out freopen("out.txt","w",stdout); #define pb push_back #define all(x) x.begin(),x.end() #define fs first #define sc second using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int maxn = 1e6+10; const int maxM = 1e6+10; const int inf = 1e8; const ll inf2 = 1e17; template<class T>void read(T &x){ T s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); x = s*w; } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template <typename ... T> void DummyWrapper(T... t){} template <class T> T unpacker(const T& t){ cout<<' '<<t; return t; } template <typename T, typename... Args> void pt(const T& t, const Args& ... data){ cout << t; DummyWrapper(unpacker(data)...); cout << '\n'; } //-------------------------------------------- int T,N,M; char G[55][55]; int dir[4][2] = {-1,0,1,0,0,1,0,-1}; struct Pos { int x,y; }; queue<Pos> q; bool vis[55][55]; bool build(){ for(int i = 1;i<=N;i++){ for(int j = 1;j<=M;j++){ if(G[i][j] != 'B') continue; for(int k = 0;k<4;k++){ int nx = i + dir[k][0]; int ny = j + dir[k][1]; if(nx<1 || nx > N || ny<1 || ny>M || G[nx][ny] == 'B') continue; if(G[nx][ny] == 'G') return 0; G[nx][ny] = '#'; } } } // for(int i = 1;i<=N;i++) puts(G[i]+1); return 1; } bool solve(){ if(G[N][M] != '#'){ q.push({N,M}); vis[N][M] = 1; } while(q.size()){ Pos cur = q.front();q.pop(); int x = cur.x,y = cur.y; for(int i = 0;i<4;i++){ int nx = x + dir[i][0]; int ny = y + dir[i][1]; if(nx<1 || nx > N || ny < 1 || ny>M || G[nx][ny] == '#'||vis[nx][ny]) continue; vis[nx][ny] = 1; q.push({nx,ny}); } } bool ok = 1; for(int i = 1;i<=N;i++){ for(int j = 1;j<=M;j++){ if(G[i][j] == 'G' && !vis[i][j]){ ok = 0; } } } return ok; } int main(){ // debug_in; read(T); while(T--){ read(N,M); for(int i = 1;i<=N;i++) for(int j = 1;j<=M;j++) vis[i][j] = 0; for(int i = 1;i<=N;i++){ scanf("%s",G[i]+1); } bool ok = 1; ok = ok & build(); ok = ok & solve(); if(ok) puts("YES"); else puts("NO"); } return 0; }