-
Notifications
You must be signed in to change notification settings - Fork 0
/
733. Flood Fill.py
34 lines (30 loc) · 1.38 KB
/
733. Flood Fill.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
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
from collections import deque
to_change = []
stack = deque()
# Initialize visted list which keeps track of already visited pixels
visited = []
num_rows = len(image)
num_cols = len(image[0])
for i in range(num_rows):
row = []
for j in range(num_cols):
row.append(False)
visited.append(row)
to_change.append((sr, sc))
stack.append((sr, sc))
while len(stack) != 0:
new_pixel = stack.pop()
if new_pixel[0] < 0 or new_pixel[1] < 0 or new_pixel[0] == num_rows or new_pixel[1] == num_cols or visited[new_pixel[0]][new_pixel[1]]:
continue
visited[new_pixel[0]][new_pixel[1]] = True
if image[new_pixel[0]][new_pixel[1]] == image[sr][sc]:
to_change.append((new_pixel[0], new_pixel[1]))
stack.append((new_pixel[0] - 1, new_pixel[1]))
stack.append((new_pixel[0] + 1, new_pixel[1]))
stack.append((new_pixel[0], new_pixel[1] - 1))
stack.append((new_pixel[0], new_pixel[1] + 1))
for affected in to_change:
image[affected[0]][affected[1]] = newColor
return image