-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path130.py
More file actions
30 lines (28 loc) · 1.04 KB
/
Copy path130.py
File metadata and controls
30 lines (28 loc) · 1.04 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
class Solution:
def solve(self, board: List[List[str]]) -> None:
def DFS(x, y):
board[x][y] = 'M'
dir_x, dir_y = [0, 1, 0, -1], [1, 0, -1, 0]
for k in range(4):
new_x, new_y = x + dir_x[k], y + dir_y[k]
if new_x >= 0 and new_x < m and new_y >= 0 and new_y < n and board[new_x][new_y] == 'O':
DFS(new_x, new_y)
m, n = len(board), len(board[0]) if len(board) > 0 else 0
if m == 0:
return
for i in range(m):
if board[i][0] == 'O':
DFS(i, 0)
if board[i][n - 1] == 'O':
DFS(i, n - 1)
for j in range(1, n - 1):
if board[0][j] == 'O':
DFS(0, j)
if board[m - 1][j] == 'O':
DFS(m - 1, j)
for i in range(m):
for j in range(n):
if board[i][j] == 'M':
board[i][j] = 'O'
elif board[i][j] == 'O':
board[i][j] = 'X'