-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSurroundedRegions.py
executable file
·49 lines (40 loc) · 1.51 KB
/
SurroundedRegions.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
from typing import List
from collections import deque
class Solution:
def solve(self, board: List[List[str]]) -> None:
if not board or not board[0]:
return
rows, cols = len(board), len(board[0])
queue = deque()
# Step 1: Add all border "O" cells to the queue
for r in range(rows):
for c in range(cols):
if (r == 0 or r == rows - 1 or c == 0 or c == cols - 1) and board[r][c] == "O":
queue.append((r, c))
# Step 2: Perform BFS to mark border-connected "O" cells as "T"
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while queue:
r, c = queue.popleft()
if board[r][c] == "O":
board[r][c] = "T"
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and board[nr][nc] == "O":
queue.append((nr, nc))
# Step 3: Flip all remaining "O" to "X" and revert "T" back to "O"
for r in range(rows):
for c in range(cols):
if board[r][c] == "O":
board[r][c] = "X"
elif board[r][c] == "T":
board[r][c] = "O"
# Test case
sol = Solution()
board = [["X", "X", "X", "X"],
["X", "O", "O", "X"],
["X", "X", "O", "X"],
["X", "O", "X", "X"]]
sol.solve(board)
# Print the result
for row in board:
print(" ".join(row))