-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130. 被围绕的区域.py
58 lines (51 loc) · 1.48 KB
/
130. 被围绕的区域.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
# -*- encoding: utf-8 -*-
'''
@File : 130. 被围绕的区域.py
@Time : 2020/04/30 10:31:27
@Author : windmzx
@Version : 1.0
@Desc : For leetcode template
'''
# here put the import lib
from typing import List
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
if board is None:
return
m = len(board)
if m == 0:
return
n = len(board[0])
visted = [[0 for _ in range(n)] for _ in range(m)]
def bfs(i, j):
if i < 0 or i >= m or j < 0 or j >= n:
return
if board[i][j] == 'O'and visted[i][j] != 1:
visted[i][j] = 1
bfs(i+1, j)
bfs(i-1, j)
bfs(i, j+1)
bfs(i, j-1)
for i in range(m):
if board[i][0] == "O":
bfs(i, 0)
if board[i][n-1] == "O":
bfs(i, n-1)
for j in range(n):
if board[0][j] == "O":
bfs(0, j)
if board[m-1][j] == "O":
bfs(m-1, j)
for i in range(m):
for j in range(n):
if visted[i][j] == 1:
board[i][j] = "O"
if __name__ == "__main__":
x = Solution()
borad = [["X", "X", "X", "X"], ["X", "O", "O", "X"], [
"X", "O", "O", "X"], ["X", "O", "X", "X"]]
x.solve(borad)
print(borad)