-
Notifications
You must be signed in to change notification settings - Fork 1
/
130. 被围绕的区域
45 lines (40 loc) · 1.37 KB
/
130. 被围绕的区域
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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"
# 学习别人的深度搜索
# 链接:https://leetcode-cn.com/problems/surrounded-regions/solution/dfs-bfs-bing-cha-ji-by-powcai/