1365D.md (2400B)
1 Source: https://codeforces.com/problemset/problem/1365/D 2 3 D. Solve The Maze 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Vivek has encountered a problem. He has a maze that can be represented as an $n \times m$ grid. Each of the grid cells may represent the following: 22 23 * Empty — '.' 24 * Wall — '#' 25 * Good person — 'G' 26 * Bad person — 'B' 27 28 The only escape from the maze is at cell $(n, m)$. 29 30 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. 31 32 Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions. 33 34 It is guaranteed that the cell $(n,m)$ is empty. Vivek can also block this cell. 35 36 Input 37 38 The first line contains one integer $t$ $(1 \le t \le 100)$ — the number of test cases. The description of the test cases follows. 39 40 The first line of each test case contains two integers $n$, $m$ $(1 \le n, m \le 50)$ — the number of rows and columns in the maze. 41 42 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. 43 44 Output 45 46 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" 47 48 You may print every letter in any case (upper or lower). 49 50 Example 51 52 Input 53 54 ``` 55 6 56 1 1 57 . 58 1 2 59 G. 60 2 2 61 #B 62 G. 63 2 3 64 G.# 65 B#. 66 3 3 67 #B. 68 #.. 69 GG. 70 2 2 71 #B 72 B. 73 ``` 74 75 Output 76 77 ``` 78 Yes 79 Yes 80 No 81 No 82 Yes 83 Yes 84 ``` 85 86 Note 87 88 For the first and second test cases, all conditions are already satisfied. 89 90 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. 91 92 For the fourth test case, the good person at $(1,1)$ cannot escape. 93 94 For the fifth test case, Vivek can block the cells $(2,3)$ and $(2,2)$. 95 96 For the last test case, Vivek can block the destination cell $(2, 2)$.