-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurrounded_regions.py
37 lines (31 loc) · 1.3 KB
/
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
class Solution(object):
def solve(self, board):
"""
:type board: List[List[str]]
:rtype: None Do not return anything, modify board in-place instead.
"""
toflip = list()
if len(board) == 0:
return board
def should_flip(i, j, board):
if i < 0 or j < 0 or i > len(board) - 1 or j > (len(board[0])) - 1:
#print("i = {}, j = {}, out of boundary".format(i, j))
return False
if board[i][j] == 'O':
board[i][j] = '-'
toflip.append((i, j))
return should_flip(i-1, j, board) and should_flip(i, j+1, board) and should_flip(i+1, j, board) and should_flip(i, j-1, board)
else:
return True
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == 'X':
continue
#print board
if should_flip(i, j, board):
for cell in toflip:
board[cell[0]][cell[1]] = 'X'
else:
for cell in toflip:
board[cell[0]][cell[1]] = 'O'
toflip = list()