题目:
题解:
class Solution:
def solve(self, board: List[List[str]]) -> None:
if not board:
return
n, m = len(board), len(board[0])
que = collections.deque()
for i in range(n):
if board[i][0] == "O":
que.append((i, 0))
board[i][0] = "A"
if board[i][m - 1] == "O":
que.append((i, m - 1))
board[i][m - 1] = "A"
for i in range(m - 1):
if board[0][i] == "O":
que.append((0, i))
board[0][i] = "A"
if board[n - 1][i] == "O":
que.append((n - 1, i))
board[n - 1][i] = "A"
while que:
x, y = que.popleft()
for mx, my in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]:
if 0 <= mx < n and 0 <= my < m and board[mx][my] == "O":
que.append((mx, my))
board[mx][my] = "A"
for i in range(n):
for j in range(m):
if board[i][j] == "A":
board[i][j] = "O"
elif board[i][j] == "O":
board[i][j] = "X"