-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path130.Surrounded-Regions.py
100 lines (80 loc) · 2.98 KB
/
130.Surrounded-Regions.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# https://leetcode.com/problems/surrounded-regions/description/
#
# algorithms
# Medium (21.17%)
# Total Accepted: 121.7K
# Total Submissions: 574.7K
class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
row = len(board)
if row <= 2:
return
col = len(board[0])
if col <= 2:
return
self.is_visited = [[False] * col for _ in xrange(row)]
for i in xrange(row):
for j in xrange(col):
if board[i][j] == 'O' and not self.is_visited[i][j]:
self.path = []
self.is_board = False
self.resursive(i, j, board, row, col)
if not self.is_board:
for x, y in self.path:
board[x][y] = 'X'
return
def resursive(self, x, y, board, row, col):
self.is_visited[x][y] = True
self.path += (x, y),
if x == 0 or x == row - 1 or y == 0 or y == col - 1:
self.is_board = True
if x > 0 and board[x - 1][y] == 'O' and not self.is_visited[x - 1][y]:
self.resursive(x - 1, y, board, row, col)
if x < row - 1 and board[x + 1][y] == 'O' and not self.is_visited[x + 1][y]:
self.resursive(x + 1, y, board, row, col)
if y > 0 and board[x][y - 1] == 'O' and not self.is_visited[x][y - 1]:
self.resursive(x, y - 1, board, row, col)
if y < col - 1 and board[x][y + 1] == 'O' and not self.is_visited[x][y + 1]:
self.resursive(x, y + 1, board, row, col)
class Solution1(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
row = len(board)
if row == 0:
return
col = len(board[0])
def recursive(i, j):
board[i][j] = 'A'
if i > 0 and board[i - 1][j] == 'O':
recursive(i - 1, j)
if i < row - 1 and board[i + 1][j] == 'O':
recursive(i + 1, j)
if j > 0 and board[i][j - 1] == 'O':
recursive(i, j - 1)
if j < col - 1 and board[i][j + 1] == 'O':
recursive(i, j + 1)
for i in xrange(row):
if board[i][0] == 'O':
recursive(i, 0)
if board[i][col - 1] == 'O':
recursive(i, col - 1)
for i in xrange(col):
if board[0][i] == 'O':
recursive(0, i)
if board[row - 1][i] == 'O':
recursive(row - 1, i)
for i in xrange(row):
for j in xrange(col):
if board[i][j] == 'O':
board[i][j] = 'X'
for i in xrange(row):
for j in xrange(col):
if board[i][j] == 'A':
board[i][j] = 'O'