-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130_Surrounded_Regions.py
More file actions
37 lines (29 loc) · 1.2 KB
/
Copy path130_Surrounded_Regions.py
File metadata and controls
37 lines (29 loc) · 1.2 KB
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
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
self.visit = set()
for i in range(0, len(board)):
for j in range(0, len(board[0])):
if board[i][j] == "O" and (i, j) not in self.visit:
# perform dfs
regions = self.dfs(board, i, j, [])
if regions != []:
# capture surrounded region
for (a, b) in regions:
board[a][b] = "X"
def dfs(self, board, i, j, path):
rows, cols = len(board), len(board[0])
self.visit.add((i, j))
path.append((i, j))
escaped = i == 0 or i == rows - 1 or j == 0 or j == cols - 1
for k, l in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
a, b = i + k, j + l
if not (0 <= a < rows and 0 <= b < cols):
continue
if board[a][b] == "X" or (a, b) in self.visit:
continue
if self.dfs(board, a, b, path) == []:
escaped = True # note: no return
return [] if escaped else path