-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFSfloodfill.py
71 lines (54 loc) · 2.26 KB
/
DFSfloodfill.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
# Below lists detail all eight possible movements
row = [-1, -1, -1, 0, 0, 1, 1, 1]
col = [-1, 0, 1, -1, 1, -1, 0, 1]
# check if it is possible to go to pixel (x, y) from the
# current pixel. The function returns false if the pixel
# has a different color, or it's not a valid pixel
def isSafe(mat, x, y, target):
return 0 <= x < len(mat) and 0 <= y < len(mat[0]) and mat[x][y] == target
# Flood fill using DFS
def floodfill(mat, x, y, replacement):
# base case
if not mat or not len(mat):
return
# get the target color
target = mat[x][y]
# print(target)
# print(replacement)
# target color is same as replacement
if target == replacement:
# print(replacement)
# input("x")
return
# replace the current pixel color with that of replacement
mat[x][y] = replacement
# process all eight adjacent pixels of the current pixel and
# recur for each valid pixel
for k in range(len(row)):
# if the adjacent pixel at position (x + row[k], y + col[k]) is
# a valid pixel and has the same color as that of the current pixel
if isSafe(mat, x + row[k], y + col[k], target):
floodfill(mat, x + row[k], y + col[k], replacement)
if __name__ == '__main__':
# matrix showing portion of the screen having different colors
mat = [
['Y', 'Y', 'Y', 'G', 'G', 'G', 'G', 'G', 'G', 'G'],
['Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'G', 'X', 'X', 'X'],
['G', 'G', 'G', 'G', 'G', 'G', 'G', 'X', 'X', 'X'],
['W', 'W', 'W', 'W', 'W', 'G', 'G', 'G', 'G', 'X'],
['W', 'R', 'R', 'R', 'R', 'R', 'G', 'X', 'X', 'X'],
['W', 'W', 'W', 'R', 'R', 'G', 'G', 'X', 'X', 'X'],
['W', 'B', 'W', 'R', 'R', 'R', 'R', 'R', 'R', 'X'],
['W', 'B', 'B', 'B', 'B', 'R', 'R', 'X', 'X', 'X'],
['W', 'B', 'B', 'X', 'B', 'B', 'B', 'B', 'X', 'X'],
['W', 'B', 'B', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
]
# start node
x, y = (3, 9) # having a target color `X`
# replacement color
replacement = 'C'
# replace the target color with a replacement color using DFS
floodfill(mat, x, y, replacement)
# print the colors after replacement
for r in mat:
print(r)