-
Notifications
You must be signed in to change notification settings - Fork 0
/
130. Surrounded Regions
62 lines (47 loc) · 1.91 KB
/
130. Surrounded Regions
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
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
__author__ = 'Wei Mu'
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if board == []:
return
keep_O, prev = [], []
keep_O.extend(self.check_outer(board))
while keep_O != prev:
prev = keep_O.copy()
keep_O.extend(self.check_near(keep_O, board))
for m in range(len(board)):
for n in range(len(board[0])):
if board[m][n] == 'O' and [m,n] not in keep_O:
board[m][n] = 'X'
def check_outer(self, board):
O_list = []
m = len(board)
n = len(board[0])
for i in range(n):
if board[0][i] == 'O':
O_list.append([0,i])
if board[m-1][i] == 'O':
O_list.append([m-1, i])
for j in range(m):
if board[j][0] == 'O':
O_list.append([j,0])
if board[j][n-1] == 'O':
O_list.append([j, n-1])
return O_list
def check_near(self, keep_O, board):
ret_list = []
for i, j in keep_O:
# check U, D, L, R
if i > 0 and board[i-1][j] == 'O' and [i - 1, j] not in keep_O + ret_list:
ret_list.append([i - 1, j])
if i < len(board) - 1 and board[i+1][j] == 'O' and [i + 1, j] not in keep_O + ret_list:
ret_list.append([i + 1, j])
if j > 0 and board[i][j - 1] == 'O' and [i, j - 1] not in keep_O + ret_list:
print([i, j-1])
ret_list.append([i, j - 1])
if j < len(board[0]) - 1 and board[i][j + 1] == 'O' and [i, j + 1] not in keep_O + ret_list:
ret_list.append([i, j + 1])
return ret_list