-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathflood_fill.py
89 lines (66 loc) · 2.1 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from collections import namedtuple
from queue import Queue
import numpy as np
Point = namedtuple("Point", "x y")
def inbounds(canvas_shape, p):
return min(p) >= 0 and p.x < canvas_shape[0] and p.y < canvas_shape[1]
def find_neighbors(canvas, p, old_val, new_val):
# north, south, east, west neighbors
possible_neighbors = [
Point(p.x, p.y+1),
Point(p.x+1, p.y),
Point(p.x-1, p.y),
Point(p.x, p.y-1)
]
# exclude the neighbors that go out of bounds and should not be colored
neighbors = []
for possible_neighbor in possible_neighbors:
if inbounds(canvas.shape, possible_neighbor):
if canvas[possible_neighbor] == old_val:
neighbors.append(possible_neighbor)
return neighbors
def stack_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return
stack = [p]
while stack:
cur_loc = stack.pop()
canvas[cur_loc] = new_val
stack += find_neighbors(canvas, cur_loc, old_val, new_val)
def queue_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return
q = Queue()
q.put(p)
canvas[p] = new_val
while not q.empty():
cur_loc = q.get()
neighbors = find_neighbors(canvas, cur_loc, old_val, new_val)
for neighbor in neighbors:
canvas[neighbor] = new_val
q.put(neighbor)
def recursive_fill(canvas, p, old_val, new_val):
if old_val == new_val:
return
canvas[p] = new_val
neighbors = find_neighbors(canvas, p, old_val, new_val)
for neighbor in neighbors:
recursive_fill(canvas, neighbor, old_val, new_val)
def main():
grid = np.zeros((5, 5))
grid[2,:] = 1
answer = np.zeros((5, 5))
answer[:3,] = 1
c0 = grid.copy()
c1 = grid.copy()
c2 = grid.copy()
start_loc = Point(0, 0)
recursive_fill(c0, start_loc, 0, 1)
queue_fill(c1, start_loc, 0, 1)
stack_fill(c2, start_loc, 0, 1)
assert (c0 == answer).all()
assert (c1 == answer).all()
assert (c2 == answer).all()
print("Tests Passed")
if __name__ == "__main__":
main()