@@ -33,7 +33,13 @@ sr = 1, sc = 1, newColor = 2
33
33
34
34
<!-- 这里可写通用的实现逻辑 -->
35
35
36
- DFS。
36
+ ** 方法一:Flood fill 算法**
37
+
38
+ Flood fill 算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。
39
+
40
+ 最简单的实现方法是采用 DFS 的递归方法,也可以采用 BFS 的迭代来实现。
41
+
42
+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为图像的行数和列数。
37
43
38
44
<!-- tabs:start -->
39
45
@@ -46,21 +52,43 @@ class Solution:
46
52
def floodFill (
47
53
self , image : List[List[int ]], sr : int , sc : int , newColor : int
48
54
) -> List[List[int ]]:
49
- def dfs (i , j , oc , nc ):
55
+ def dfs (i , j ):
50
56
if (
51
- i < 0
52
- or i >= len (image)
53
- or j < 0
54
- or j >= len (image[0 ])
57
+ not 0 <= i < m
58
+ or not 0 <= j < n
55
59
or image[i][j] != oc
56
- or image[i][j] == nc
60
+ or image[i][j] == newColor
57
61
):
58
62
return
59
- image[i][j] = nc
60
- for x, y in [[0 , 1 ], [0 , - 1 ], [1 , 0 ], [- 1 , 0 ]]:
61
- dfs(i + x, j + y, oc, nc)
63
+ image[i][j] = newColor
64
+ for a, b in pairwise(dirs):
65
+ dfs(i + a, j + b)
66
+
67
+ dirs = (- 1 , 0 , 1 , 0 , - 1 )
68
+ m, n = len (image), len (image[0 ])
69
+ oc = image[sr][sc]
70
+ dfs(sr, sc)
71
+ return image
72
+ ```
62
73
63
- dfs(sr, sc, image[sr][sc], newColor)
74
+ ``` python
75
+ class Solution :
76
+ def floodFill (
77
+ self , image : List[List[int ]], sr : int , sc : int , newColor : int
78
+ ) -> List[List[int ]]:
79
+ if image[sr][sc] == newColor:
80
+ return image
81
+ q = deque([(sr, sc)])
82
+ oc = image[sr][sc]
83
+ image[sr][sc] = newColor
84
+ dirs = (- 1 , 0 , 1 , 0 , - 1 )
85
+ while q:
86
+ i, j = q.popleft()
87
+ for a, b in pairwise(dirs):
88
+ x, y = i + a, j + b
89
+ if 0 <= x < len (image) and 0 <= y < len (image[0 ]) and image[x][y] == oc:
90
+ q.append((x, y))
91
+ image[x][y] = newColor
64
92
return image
65
93
```
66
94
@@ -70,22 +98,55 @@ class Solution:
70
98
71
99
``` java
72
100
class Solution {
73
- private int [][] dirs = new int [][] {{0 , 1 }, {0 , - 1 }, {1 , 0 }, {- 1 , 0 }};
101
+ private int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
102
+ private int [][] image;
103
+ private int nc;
104
+ private int oc;
74
105
75
106
public int [][] floodFill (int [][] image , int sr , int sc , int newColor ) {
76
- dfs(image, sr, sc, image[sr][sc], newColor);
107
+ nc = newColor;
108
+ oc = image[sr][sc];
109
+ this . image = image;
110
+ dfs(sr, sc);
77
111
return image;
78
112
}
79
113
80
- private void dfs (int [][] image , int i , int j , int oc , int nc ) {
81
- if (i < 0 || i >= image. length || j < 0 || j >= image[0 ]. length || image[i][j] != oc
82
- || image[i][j] == nc) {
114
+ private void dfs (int i , int j ) {
115
+ if (i < 0 || i >= image. length || j < 0 || j >= image[0 ]. length || image[i][j] != oc || image[i][j] == nc) {
83
116
return ;
84
117
}
85
118
image[i][j] = nc;
86
- for (int [] dir : dirs) {
87
- dfs(image, i + dir[0 ], j + dir[1 ], oc, nc);
119
+ for (int k = 0 ; k < 4 ; ++ k) {
120
+ dfs(i + dirs[k], j + dirs[k + 1 ]);
121
+ }
122
+ }
123
+ }
124
+ ```
125
+
126
+ ``` java
127
+ class Solution {
128
+ public int [][] floodFill (int [][] image , int sr , int sc , int newColor ) {
129
+ if (image[sr][sc] == newColor) {
130
+ return image;
131
+ }
132
+ Deque<int[]> q = new ArrayDeque<> ();
133
+ q. offer(new int [] {sr, sc});
134
+ int oc = image[sr][sc];
135
+ image[sr][sc] = newColor;
136
+ int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
137
+ while (! q. isEmpty()) {
138
+ int [] p = q. poll();
139
+ int i = p[0 ], j = p[1 ];
140
+ for (int k = 0 ; k < 4 ; ++ k) {
141
+ int x = i + dirs[k], y = j + dirs[k + 1 ];
142
+ if (x >= 0 && x < image. length && y >= 0 && y < image[0 ]. length
143
+ && image[x][y] == oc) {
144
+ q. offer(new int [] {x, y});
145
+ image[x][y] = newColor;
146
+ }
147
+ }
88
148
}
149
+ return image;
89
150
}
90
151
}
91
152
```
@@ -95,17 +156,48 @@ class Solution {
95
156
``` cpp
96
157
class Solution {
97
158
public:
98
- vector<vector<int >> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
99
-
100
159
vector<vector<int >> floodFill(vector<vector<int >>& image, int sr, int sc, int newColor) {
101
- dfs(image, sr, sc, image[sr][sc], newColor);
160
+ int m = image.size(), n = image[ 0] .size();
161
+ int oc = image[ sr] [ sc ] ;
162
+ int dirs[ 5] = {-1, 0, 1, 0, -1};
163
+ function<void(int, int)> dfs = [ &] (int i, int j) {
164
+ if (i < 0 || i >= m || j < 0 || j >= n || image[ i] [ j ] != oc || image[ i] [ j ] == newColor) {
165
+ return;
166
+ }
167
+ image[ i] [ j ] = newColor;
168
+ for (int k = 0; k < 4; ++k) {
169
+ dfs(i + dirs[ k] , j + dirs[ k + 1] );
170
+ }
171
+ };
172
+ dfs(sr, sc);
102
173
return image;
103
174
}
175
+ };
176
+ ```
104
177
105
- void dfs (vector<vector<int >>& image, int i, int j, int oc, int nc) {
106
- if (i < 0 || i >= image.size() || j < 0 || j >= image[ 0] .size() || image[ i] [ j ] != oc || image[ i] [ j ] == nc) return;
107
- image[ i] [ j ] = nc;
108
- for (auto& dir : dirs) dfs(image, i + dir[ 0] , j + dir[ 1] , oc, nc);
178
+ ```cpp
179
+ class Solution {
180
+ public:
181
+ vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
182
+ if (image[sr][sc] == newColor) return image;
183
+ int oc = image[sr][sc];
184
+ image[sr][sc] = newColor;
185
+ queue<pair<int, int>> q;
186
+ q.push({sr, sc});
187
+ int dirs[5] = {-1, 0, 1, 0, -1};
188
+ while (!q.empty()) {
189
+ auto [a, b] = q.front();
190
+ q.pop();
191
+ for (int k = 0; k < 4; ++k) {
192
+ int x = a + dirs[k];
193
+ int y = b + dirs[k + 1];
194
+ if (x >= 0 && x < image.size() && y >= 0 && y < image[0].size() && image[x][y] == oc) {
195
+ q.push({x, y});
196
+ image[x][y] = newColor;
197
+ }
198
+ }
199
+ }
200
+ return image;
109
201
}
110
202
};
111
203
```
@@ -114,19 +206,45 @@ public:
114
206
115
207
``` go
116
208
func floodFill (image [][]int , sr int , sc int , newColor int ) [][]int {
117
- dfs(image, sr, sc, image[sr][sc], newColor)
209
+ oc := image[sr][sc]
210
+ m , n := len (image), len (image[0 ])
211
+ dirs := []int {-1 , 0 , 1 , 0 , -1 }
212
+ var dfs func (i, j int )
213
+ dfs = func (i, j int ) {
214
+ if i < 0 || i >= m || j < 0 || j >= n || image[i][j] != oc || image[i][j] == newColor {
215
+ return
216
+ }
217
+ image[i][j] = newColor
218
+ for k := 0 ; k < 4 ; k++ {
219
+ dfs (i+dirs[k], j+dirs[k+1 ])
220
+ }
221
+ }
222
+ dfs (sr, sc)
118
223
return image
119
224
}
225
+ ```
120
226
121
- func dfs(image [][]int, i, j, oc, nc int) {
122
- if i < 0 || i >= len(image) || j < 0 || j >= len(image[0]) || image[i][j] != oc || image[i][j] == nc {
123
- return
227
+ ``` go
228
+ func floodFill (image [][]int , sr int , sc int , newColor int ) [][]int {
229
+ if image[sr][sc] == newColor {
230
+ return image
124
231
}
125
- image[i][j] = nc
126
- dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
127
- for _, dir := range dirs {
128
- dfs(image, i+dir[0], j+dir[1], oc, nc)
232
+ oc := image[sr][sc]
233
+ q := [][]int {[]int {sr, sc}}
234
+ image[sr][sc] = newColor
235
+ dirs := []int {-1 , 0 , 1 , 0 , -1 }
236
+ for len (q) > 0 {
237
+ p := q[0 ]
238
+ q = q[1 :]
239
+ for k := 0 ; k < 4 ; k++ {
240
+ x , y := p[0 ]+dirs[k], p[1 ]+dirs[k+1 ]
241
+ if x >= 0 && x < len (image) && y >= 0 && y < len (image[0 ]) && image[x][y] == oc {
242
+ q = append (q, []int {x, y})
243
+ image[x][y] = newColor
244
+ }
245
+ }
129
246
}
247
+ return image
130
248
}
131
249
```
132
250
0 commit comments