"""# ### 谜题描述 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. 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). 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). Here is a reference code to solve this task. You can use this to help you genereate cases or validate the solution. ```python def getAdj(x, y, N, M) : moves = [(-1, 0), (1, 0), (0, -1), (0, 1)] adj = [] for dx, dy in moves : if x+dx < 0 or x+dx >= M : continue if y+dy < 0 or y+dy >= N : continue adj.append((x+dx,y+dy)) return adj T = input() for t in range(T) : N, M = map(int,raw_input().split()) good, bad = set(), [] grid = [] for n in range(N) : row = list(raw_input()) grid.append(row) for m in range(M) : if row[m] == 'G' : good.add((n,m)) if row[m] == 'B' : bad.append((n,m)) valid = True for n, m in bad : if N-n + M-m <= 3 : valid = False adj = getAdj(m,n,N,M) for x, y in adj : if grid[y][x] == '#' : continue elif grid[y][x] == '.' : grid[y][x] = '#' if not valid : break if not valid and len(good) == 0 : print \"Yes\" elif not valid and len(good) > 0 : print \"No\" else : marked = [[False]*M for n in range(N)] q = [(N-1,M-1)] marked[N-1][M-1] = True while q : curY, curX = q.pop() if not valid: break for x, y in getAdj(curX, curY, N, M) : if grid[y][x] == '#' : continue if grid[y][x] == 'B' : valid = False if marked[y][x] : continue marked[y][x] = True good.discard((y,x)) q.append((y,x)) if len(good) > 0 : valid = False print \"Yes\" if valid else \"No\" ``` 请完成上述谜题的训练场环境类实现,包括所有必要的方法。 """ from bootcamp import Basebootcamp import random from collections import deque from bootcamp import Basebootcamp def get_adj(x, y, n_rows, m_cols): return [(nx, ny) for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)] if 0<=(nx:=x+dx) str: grid_str = '\n'.join(''.join(row) for row in question_case['grid']) return f"""Given a {question_case['n']}x{question_case['m']} maze grid where: - '.' = empty cell - '#' = wall - 'G' = good person - 'B' = bad person Exit is at bottom-right cell ({question_case['n']}, {question_case['m']}). Can we block cells (turn '.' to '#') such that: 1. All G can reach exit 2. All B cannot reach exit Maze layout: {grid_str} Answer with [answer]Yes[/answer] or [answer]No[/answer].""" @staticmethod def extract_output(output): import re matches = re.findall(r'\[answer\](yes|no)\[/answer\]', output, re.I) return matches[-1].title() if matches else None @classmethod def _verify_correction(cls, solution, identity): return solution == solve_maze(identity['n'], identity['m'], identity['grid'])