-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130_surrReg.py
52 lines (37 loc) · 1.32 KB
/
130_surrReg.py
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
46
47
48
49
50
51
52
# 130. Surrounded Regions
def dfs(board, i, j, res):
def isValid(i,j):
if i>=0 and i<len(board) and j>=0 and j<len(board[i]):
return True
else:
return False
if isValid(i,j):
if board[i][j]=='O' and (i,j) not in res:
res.append((i,j))
dfs(board, i-1, j, res)
dfs(board, i+1, j, res)
dfs(board, i, j-1, res)
dfs(board, i, j+1, res)
else:
return
else:
return
return
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
Os = []
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j]=='O':
Os.append((i,j))
res = []
for i in range(len(Os)):
if (Os[i][0]==0 or Os[i][0]==len(board)-1) or (Os[i][1]==0 or Os[i][1]==len(board[Os[i][0]])-1):
# res.append((Os[i][0], Os[i][1]))
dfs(board, Os[i][0], Os[i][1], res)
for i in range(len(Os)):
if Os[i] not in res:
board[Os[i][0]][Os[i][1]]='X'