forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0130.py
30 lines (23 loc) · 832 Bytes
/
0130.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
class Solution:
def solve(self, board: List[List[str]]) -> None:
if not board:
return
m = len(board)
n = len(board[0])
for i in range(m):
self.dfs(board, i, 0)
self.dfs(board, i, n - 1)
for j in range(1, n - 1):
self.dfs(board, 0, j)
self.dfs(board, m - 1, j)
for i in range(m):
for j in range(n):
board[i][j] = 'O' if board[i][j] == '.' else 'X'
def dfs(self, board: List[List[str]], i: int, j: int) -> None:
if i < 0 or j < 0 or i >= len(board) or j >= len(board[0]) or board[i][j] != 'O':
return
board[i][j] = '.'
self.dfs(board, i, j + 1)
self.dfs(board, i, j - 1)
self.dfs(board, i + 1, j)
self.dfs(board, i - 1, j)