-
Notifications
You must be signed in to change notification settings - Fork 0
/
130.surrounded-regions.py
51 lines (40 loc) · 1.38 KB
/
130.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
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# @lc app=leetcode id=130 lang=python3
#
# [130] Surrounded Regions
#
# @lc code=start
import collections
from typing import List
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# thinking negatively: all the border O and neighbors are not surroundable
# => all the other O are surrounded regions
# BFS
if not board:
return
m, n = len(board), len(board[0])
q = collections.deque()
# get all the positions of O on border
for r in range(m):
for c in range(n):
if (r in [0, m - 1] or c in [0, n - 1]) and board[r][c] == "O":
q.append((r, c))
# bfs, protect these border O and neighbors by converting first to 'D'
while q:
x, y = q.popleft()
if 0 <= x < m and 0 <= y < n and board[x][y] == "O":
board[x][y] = "D"
for dx, dy in [(-1, 0), (1, 0), (0, 1), (0, -1)]:
q.append((x + dx, y + dy))
# convert the rest of O to X and protected D to O
for r in range(m):
for c in range(n):
if board[r][c] == "O":
board[r][c] = "X"
if board[r][c] == "D":
board[r][c] = "O"
# @lc code=end