-
Notifications
You must be signed in to change notification settings - Fork 44
/
flood-fill.py
184 lines (158 loc) · 5.78 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
733. Flood Fill
Easy
An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.
You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor.
Return the modified image after performing the flood fill.
Example 1:
Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
Output: [[2,2,2],[2,2,2]]
Constraints:
m == image.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], newColor < 216
0 <= sr < m
0 <= sc < n
"""
# V0
# IDEA : DFS
# dfs
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
def dfs(x, y):
#print ("x = " + str(x) + " y = " + str(y))
moves = [[0,-1],[0,1],[-1,0],[1,0]]
for m in moves:
_x = x + m[1]
_y = y + m[0]
if 0 <= _x < w and 0 <= _y < l and image[_y][_x] == curColor and [_x, _y] not in visited:
visited.append([_x, _y])
image[_y][_x] = newColor
dfs(_x, _y)
# edge case
if not image:
return
visited = []
l = len(image)
w = len(image[0])
# start point : (sr, sc)
# sr : "y"
# sc : "x"
curColor = image[sr][sc]
image[sr][sc] = newColor
dfs(sc, sr)
return image
# V0'
# IDEA : DFS
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def dfs(image, r, c, newColor, color):
if (0 <= r < len(image) and \
0 <= c < len(image[0]) and \
image[r][c] == color):
for d in directions:
dfs(image, r+d[0], c+d[1], newColor, color)
image[r][c] = newColor
else:
return
color = image[sr][sc]
if color == newColor: return image
dfs(image, sr, sc, newColor, color)
return image
# V1'
# https://www.jiuzhang.com/solution/flood-fill/#tag-highlight-lang-python
# IDEA : DFS
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
rows, cols, orig_color = len(image), len(image[0]), image[sr][sc]
def traverse(row, col):
if (not (0 <= row < rows and 0 <= col < cols)) or image[row][col] != orig_color:
return
image[row][col] = newColor
[traverse(row + x, col + y) for (x, y) in ((0, 1), (1, 0), (0, -1), (-1, 0))]
if orig_color != newColor:
traverse(sr, sc)
return image
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79401716
# IDEA : DFS
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
SR, SC = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r - 1, c)
if r < SR - 1: dfs(r + 1, c)
if c >= 1: dfs(r, c - 1)
if c < SC - 1: dfs(r, c + 1)
dfs(sr, sc)
return image
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/79401716
# IDEA : BFS
class Solution:
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
que = collections.deque()
que.append((sr, sc))
start = image[sr][sc]
if start == newColor: return image
M, N = len(image), len(image[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while que:
pos = que.popleft()
image[pos[0]][pos[1]] = newColor
for d in directions:
newx, newy = pos[0] + d[0], pos[1] + d[1]
if 0 <= newx < M and 0 <= newy < N and image[newx][newy] == start:
que.append((newx, newy))
return image
# V2
# Time: O(m * n)
# Space: O(m * n)
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
def dfs(image, r, c, newColor, color):
if not (0 <= r < len(image) and \
0 <= c < len(image[0]) and \
image[r][c] == color):
return
image[r][c] = newColor
for d in directions:
dfs(image, r+d[0], c+d[1], newColor, color)
color = image[sr][sc]
if color == newColor: return image
dfs(image, sr, sc, newColor, color)
return image