-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN130被包围的区域.py
More file actions
48 lines (40 loc) · 1.14 KB
/
N130被包围的区域.py
File metadata and controls
48 lines (40 loc) · 1.14 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import List
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m, n = len(board), len(board[0])
def dfs(i, j):
if board[i][j] == 'O':
board[i][j] = 'R'
else:
return
if i + 1 < m:
dfs(i + 1, j)
if i - 1 >= 0:
dfs(i - 1, j)
if j + 1 < n:
dfs(i, j + 1)
if j - 1 >= 0:
dfs(i, j - 1)
for i in range(m):
if board[i][0] == 'O':
dfs(i, 0)
if board[i][n - 1] == 'O':
dfs(i, n - 1)
for i in range(n):
if board[0][i] == 'O':
dfs(0, i)
if board[m - 1][i] == 'O':
dfs(m - 1, i)
for i in range(m):
for j in range(n):
if board[i][j] == 'R':
board[i][j] = 'O'
elif board[i][j] == 'O':
board[i][j] = 'X'
s = Solution()
b = [["X"]]
print(s.solve(b))
print(b)