## 示例:

``````X X X X
X O O X
X X O X
X O X X``````

``````X X X X
X X X X
X X X X
X O X X``````

## 代码:

``````class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if not board or not board[0]:
return
row = len(board)
col = len(board[0])

def dfs(i, j):
board[i][j] = "B"
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
tmp_i = i + x
tmp_j = j + y
if 1 <= tmp_i < row and 1 <= tmp_j < col and board[tmp_i][tmp_j] == "O":
dfs(tmp_i, tmp_j)

for j in range(col):
# 第一行
if board[0][j] == "O":
dfs(0, j)
# 最后一行
if board[row - 1][j] == "O":
dfs(row - 1, j)

for i in range(row):
# 第一列
if board[i][0] == "O":
dfs(i, 0)
# 最后一列
if board[i][col-1] == "O":
dfs(i, col - 1)

for i in range(row):
for j in range(col):
# O 变成 X
if board[i][j] == "O":
board[i][j] = "X"
# B 变成 O
if board[i][j] == "B":
board[i][j] = "O"``````

java

``````class Solution {
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) return;
int row = board.length;
int col = board[0].length;
for (int j = 0; j < col; j++) {
// 第一行
if (board[0][j] == 'O') dfs(0, j, board, row, col);
// 最后一行
if (board[row - 1][j] == 'O') dfs(row - 1, j, board, row, col);
}

for (int i = 0; i < row; i++) {
// 第一列
if (board[i][0] == 'O') dfs(i, 0, board, row, col);
// 最后一列
if (board[i][col - 1] == 'O') dfs(i, col - 1, board, row, col);
}

// 转变
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == 'B') board[i][j] = 'O';
}
}

}

private void dfs(int i, int j, char[][] board, int row, int col) {
board[i][j] = 'B';
for (int[] dir : dirs) {
int tmp_i = dir[0] + i;
int tmp_j = dir[1] + j;
if (tmp_i < 0 || tmp_i >= row || tmp_j < 0 || tmp_j >= col || board[tmp_i][tmp_j] != 'O') continue;
dfs(tmp_i, tmp_j, board, row, col);
}
}
}``````

``````class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if not board or not board[0]:
return
row = len(board)
col = len(board[0])

def bfs(i, j):
from collections import deque
queue = deque()
queue.appendleft((i, j))
while queue:
i, j = queue.pop()
if 0 <= i < row and 0 <= j < col and board[i][j] == "O":
board[i][j] = "B"
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
queue.appendleft((i + x, j + y))

for j in range(col):
# 第一行
if board[0][j] == "O":
bfs(0, j)
# 最后一行
if board[row - 1][j] == "O":
bfs(row - 1, j)

for i in range(row):

if board[i][0] == "O":
bfs(i, 0)
if board[i][col - 1] == "O":
bfs(i, col - 1)

for i in range(row):
for j in range(col):
if board[i][j] == "O":
board[i][j] = "X"
if board[i][j] == "B":
board[i][j] = "O"``````

java

``````class Solution {
int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private static class Point {
int x, y;

Point(int x, int y) {
this.x = x;
this.y = y;
}
}

public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) return;
int row = board.length;
int col = board[0].length;
for (int j = 0; j < col; j++) {
// 第一行
if (board[0][j] == 'O') bfs(0, j, board, row, col);
// 最后一行
if (board[row - 1][j] == 'O') bfs(row - 1, j, board, row, col);
}

for (int i = 0; i < row; i++) {
// 第一列
if (board[i][0] == 'O') bfs(i, 0, board, row, col);
// 最后一列
if (board[i][col - 1] == 'O') bfs(i, col - 1, board, row, col);
}

// 转变
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (board[i][j] == 'O') board[i][j] = 'X';
if (board[i][j] == 'B') board[i][j] = 'O';
}
}

}

private void bfs(int i, int j, char[][] board, int row, int col) {
queue.offer(new Point(i, j));
while (!queue.isEmpty()) {
Point tmp = queue.poll();
if (tmp.x >= 0 && tmp.x < row && tmp.y >= 0 && tmp.y < col && board[tmp.x][tmp.y] == 'O') {
board[tmp.x][tmp.y] = 'B';
for (int[] dir : dirs) queue.offer(new Point(tmp.x + dir[0], tmp.y + dir[1]));
}
}
}
}``````

``````class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
f = {}
def find(x):
f.setdefault(x, x)
if f[x] != x:
f[x] = find(f[x])
return f[x]
def union(x, y):
f[find(y)] = find(x)

if not board or not board[0]:
return
row = len(board)
col = len(board[0])
dummy = row * col
for i in range(row):
for j in range(col):
if board[i][j] == "O":
if i == 0 or i == row - 1 or j == 0 or j == col - 1:
union(i * col + j, dummy)
else:
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if board[i + x][j + y] == "O":
union(i * col + j, (i + x) * col + (j + y))
for i in range(row):
for j in range(col):
if find(dummy) == find(i * col + j):
board[i][j] = "O"
else:
board[i][j] = "X"``````