Codeforces648div2D

Codeforces648div2D 原题: Vivek has encountered a problem. He has a maze that can be represented as an n×m 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 (n,m).
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.
【Codeforces648div2D】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 (n,m) is empty. Vivek can also block this cell.
Input
The first line contains one integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n, m (1≤n,m≤50) — the number of rows and columns in the maze.
Each of the next n lines contain m 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).
思路:先把坏人四周堵起来,如果四周有好人,直接输出no;然后再从终点bfs好人,看能否搜到所有好人。注意坏人相邻的情况,否则会WA4 ACcodes

#include using namespace std; const int N = 5e5+10; int n,m; char ch[55][55]; int g; typedef pair P; bool cmp(int a, int b) { return a>b; } int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; void bfs(int i, int j) { if(i<0||i>=n||j<0||j>=m||ch[i][j]=='#') return; if(ch[i][j]=='G') { g--; ch[i][j]='.'; } for(int x=0; x<4; x++) { int x0 = i+dx[x]; int y0 = j+dy[x]; bfs(x0,y0); } } int main() { int test=1; scanf("%d",&test); while(test--) { g=0; scanf("%d%d",&n,&m); for(int i=0; i>ch[i][j]; if(ch[i][j]=='G') g++; } } bool flag=true; for(int i=0; i=n||y0<0||y0>=m||ch[x0][y0]=='B') continue; if(ch[x0][y0]=='G') { flag = false; break; } else ch[x0][y0] = '#'; } } } } if(g==0) { cout << "YES" << endl; continue; } if(!flag || ch[n-1][m-1]=='#') { cout << "NO" << endl; continue; } queue q; q.push(P(n-1,m-1)); ch[n-1][m-1]='#'; while(q.size()) { P cnt = q.front(); q.pop(); for(int i=0; i<4; i++) { int x0 = dx[i]+cnt.first; int y0 = dy[i]+cnt.second; if(x0>=0&&x0=0&&y0

    推荐阅读