'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
示例 2:
输入:board = [["X"]]
输出:[["X"]]
提示:
m == board.length
n == board[i].length
1 <= m, n <= 200
board[i][j] 为 'X' 或 'O'
链接:https://leetcode-cn.com/problems/surrounded-regions
class Solution:
def solve(self, board: List[List[str]]) -> None:
class Solution:
def solve(self, borad: List[List[str]]) -> None:
row, col = len(borad), len(board[0])
def dfs(x, y):
if board[x][y] == 'X':
return
else:
board[x][y] = '#'
for c in [(0,1),(0,-1),(-1,0),(-1,1)]:
nx, ny = x + c[0], y + c[1]
if 0 <= nx < row and 0 <= ny < col:
dfs(nx, ny)
for i in range(row):
dfs(i, 0)
dfs(i, col-1)
for j in range(1, col-1):
dfs(0, j)
dfs(row-1, j)
for i in range(row):
for j in range(col):
if board[x][y] == '#':
board[x][y] = 'O'
else:
board[x][y] = 'X'