class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ que = [] front = -1 tail = -1 def push_back(que, front, tail, ele): lq = len(que) tail += 1 if lq <= tail: que.append(ele) else: que[tail] = ele return tail def pop(que, front, tail): front += 1 return (que[front], front) d = [(1,0),(-1,0),(0,1),(0,-1)] height = len(board) width = len(board[0]) def is_valid(x, y): if x<0 or y < 0 or x >= height or y >= width: return False return True is_visited = [] for x, l in enumerate(board): is_visited.append([]) for y in l: is_visited[x].append(0) if height < 3 or width < 3: return board def is_edge(x, y): if x == 0 or y == 0 or x == height - 1 or y == width - 1: return True return False for x in range(1,height - 1): for y in range(1,width - 1): if board[x][y] == 'X' or is_visited[x][y] == 1: continue tail = push_back(que,front, tail,(x,y)) flag = 0 last_front = front while front != tail: (top_ele, front) = pop(que, front, tail) for (cx, cy) in d: nx = cx + top_ele[0] ny = cy + top_ele[1] if is_edge(nx, ny): if board[nx][ny] == 'O': flag = 1 continue if board[nx][ny] == 'O' and is_visited[nx][ny]==0: tail = push_back(que,front,tail,(nx,ny)) is_visited[nx][ny]=1 if flag == 0: for idx in range(last_front + 1, front + 1): xx = que[idx][0] yy = que[idx][1] board[xx][yy]='X' return board