-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path130.py
29 lines (27 loc) · 1.08 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
# https://neetcode.io/problems/surrounded-regions
# https://leetcode.com/problems/surrounded-regions/description/
from typing import List
class Solution:
def solve(self, board: List[List[str]]) -> None:
ROW, COL = len(board), len(board[0])
visited = set()
def surroundedByX(r, c) -> bool:
if r < 0 or c < 0 or r >= ROW or c >= COL:
return False
if board[r][c] == 'X' or (r, c) in visited:
return True
visited.add((r, c))
top = surroundedByX(r - 1, c)
bot = surroundedByX(r + 1, c)
left = surroundedByX(r, c - 1)
right = surroundedByX(r, c + 1)
print(r, c, top and bot and left and right)
return top and bot and left and right
def fillX(coords):
for r, c in coords:
board[r][c] = 'X'
for r in range(ROW):
for c in range(COL):
if board[r][c] == 'O' and (r, c) not in visited and surroundedByX(r, c):
fillX(visited)
visited = set()