@@ -45,14 +45,38 @@ grid2 中标红的 1 区域是子岛屿,总共有 2 个子岛屿。
45
45
46
46
<!-- 这里可写通用的实现逻辑 -->
47
47
48
- 深度优先搜索, 或者并查集。
48
+ BFS、DFS 或者并查集。
49
49
50
50
<!-- tabs:start -->
51
51
52
52
### ** Python3**
53
53
54
54
<!-- 这里可写当前语言的特殊实现逻辑 -->
55
55
56
+ BFS - Flood Fill 算法:
57
+
58
+ ``` python
59
+ class Solution :
60
+ def countSubIslands (self , grid1 : List[List[int ]], grid2 : List[List[int ]]) -> int :
61
+ def bfs (i , j ):
62
+ q = deque([(i, j)])
63
+ grid2[i][j] = 0
64
+ ans = True
65
+ while q:
66
+ i, j = q.popleft()
67
+ if grid1[i][j] == 0 :
68
+ ans = False
69
+ for a, b in [[0 , - 1 ], [0 , 1 ], [1 , 0 ], [- 1 , 0 ]]:
70
+ x, y = i + a, j + b
71
+ if 0 <= x < m and 0 <= y < n and grid2[x][y]:
72
+ q.append((x, y))
73
+ grid2[x][y] = 0
74
+ return ans
75
+
76
+ m, n = len (grid1), len (grid1[0 ])
77
+ return sum (grid2[i][j] and bfs(i, j) for i in range (m) for j in range (n))
78
+ ```
79
+
56
80
DFS:
57
81
58
82
``` python
@@ -76,27 +100,24 @@ class Solution:
76
100
``` python
77
101
class Solution :
78
102
def countSubIslands (self , grid1 : List[List[int ]], grid2 : List[List[int ]]) -> int :
79
- m, n = len (grid1), len (grid1[0 ])
80
- p = list (range (m * n))
81
-
82
103
def find (x ):
83
104
if p[x] != x:
84
105
p[x] = find(p[x])
85
106
return p[x]
86
-
107
+
108
+ m, n = len (grid1), len (grid1[0 ])
109
+ p = list (range (m * n))
87
110
for i in range (m):
88
111
for j in range (n):
89
- if grid2[i][j] == 1 :
90
- idx = i * n + j
91
- if i < m - 1 and grid2[i + 1 ][j] == 1 :
92
- p[find(idx)] = find((i + 1 ) * n + j)
93
- if j < n - 1 and grid2[i][j + 1 ] == 1 :
94
- p[find(idx)] = find(i * n + j + 1 )
95
-
112
+ if grid2[i][j]:
113
+ for a, b in [[0 , 1 ], [1 , 0 ]]:
114
+ x, y = i + a, j + b
115
+ if x < m and y < n and grid2[x][y]:
116
+ p[find(x * n + y)] = find(i * n + j)
96
117
s = [0 ] * (m * n)
97
118
for i in range (m):
98
119
for j in range (n):
99
- if grid2[i][j] == 1 :
120
+ if grid2[i][j]:
100
121
s[find(i * n + j)] = 1
101
122
for i in range (m):
102
123
for j in range (n):
@@ -110,6 +131,51 @@ class Solution:
110
131
111
132
<!-- 这里可写当前语言的特殊实现逻辑 -->
112
133
134
+ BFS - Flood Fill 算法:
135
+
136
+ ``` java
137
+ class Solution {
138
+ public int countSubIslands (int [][] grid1 , int [][] grid2 ) {
139
+ int m = grid1. length;
140
+ int n = grid1[0 ]. length;
141
+ int ans = 0 ;
142
+ for (int i = 0 ; i < m; ++ i) {
143
+ for (int j = 0 ; j < n; ++ j) {
144
+ if (grid2[i][j] == 1 && bfs(i, j, m, n, grid1, grid2)) {
145
+ ++ ans;
146
+ }
147
+ }
148
+ }
149
+ return ans;
150
+ }
151
+
152
+ private boolean bfs (int i , int j , int m , int n , int [][] grid1 , int [][] grid2 ) {
153
+ Queue<int[]> q = new ArrayDeque<> ();
154
+ grid2[i][j] = 0 ;
155
+ q. offer(new int []{i, j});
156
+ boolean ans = true ;
157
+ int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
158
+ while (! q. isEmpty()) {
159
+ int [] p = q. poll();
160
+ i = p[0 ];
161
+ j = p[1 ];
162
+ if (grid1[i][j] == 0 ) {
163
+ ans = false ;
164
+ }
165
+ for (int k = 0 ; k < 4 ; ++ k) {
166
+ int x = i + dirs[k];
167
+ int y = j + dirs[k + 1 ];
168
+ if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] == 1 ) {
169
+ q. offer(new int []{x, y});
170
+ grid2[x][y] = 0 ;
171
+ }
172
+ }
173
+ }
174
+ return ans;
175
+ }
176
+ }
177
+ ```
178
+
113
179
DFS:
114
180
115
181
``` java
@@ -151,47 +217,47 @@ class Solution {
151
217
private int [] p;
152
218
153
219
public int countSubIslands (int [][] grid1 , int [][] grid2 ) {
154
- int m = grid2. length, n = grid2[0 ]. length;
220
+ int m = grid1. length;
221
+ int n = grid1[0 ]. length;
155
222
p = new int [m * n];
156
223
for (int i = 0 ; i < p. length; ++ i) {
157
224
p[i] = i;
158
225
}
226
+ int [] dirs = {1 , 0 , 1 };
159
227
for (int i = 0 ; i < m; ++ i) {
160
228
for (int j = 0 ; j < n; ++ j) {
161
229
if (grid2[i][j] == 1 ) {
162
- int idx = i * n + j;
163
- if (i < m - 1 && grid2[ i + 1 ][j] == 1 ) {
164
- p[find(idx)] = find((i + 1 ) * n + j) ;
165
- }
166
- if (j < n - 1 && grid2[i][j + 1 ] == 1 ) {
167
- p[find(idx)] = find(i * n + j + 1 );
230
+ for ( int k = 0 ; k < 2 ; ++ k) {
231
+ int x = i + dirs[k];
232
+ int y = j + dirs[k + 1 ] ;
233
+ if (x < m && y < n && grid2[x][y] == 1 ) {
234
+ p[find(x * n + y) ] = find(i * n + j);
235
+ }
168
236
}
169
237
}
170
238
}
171
239
}
172
- boolean [] s = new boolean [m * n];
240
+ int [] s = new int [m * n];
173
241
for (int i = 0 ; i < m; ++ i) {
174
242
for (int j = 0 ; j < n; ++ j) {
175
243
if (grid2[i][j] == 1 ) {
176
- s[find(i * n + j)] = true ;
244
+ s[find(i * n + j)] = 1 ;
177
245
}
178
246
}
179
247
}
180
248
for (int i = 0 ; i < m; ++ i) {
181
249
for (int j = 0 ; j < n; ++ j) {
182
250
int root = find(i * n + j);
183
- if (s[root] && grid1[i][j] == 0 ) {
184
- s[root] = false ;
251
+ if (s[root] == 1 && grid1[i][j] == 0 ) {
252
+ s[root] = 0 ;
185
253
}
186
254
}
187
255
}
188
- int res = 0 ;
189
- for (boolean e : s) {
190
- if (e) {
191
- ++ res;
192
- }
256
+ int ans = 0 ;
257
+ for (int i = 0 ; i < s. length; ++ i) {
258
+ ans += s[i];
193
259
}
194
- return res ;
260
+ return ans ;
195
261
}
196
262
197
263
private int find (int x ) {
@@ -254,6 +320,51 @@ function dfs(
254
320
255
321
### ** C++**
256
322
323
+ BFS - Flood Fill 算法:
324
+
325
+ ``` cpp
326
+ class Solution {
327
+ public:
328
+ int countSubIslands(vector<vector<int >>& grid1, vector<vector<int >>& grid2) {
329
+ int m = grid1.size();
330
+ int n = grid1[ 0] .size();
331
+ int ans = 0;
332
+ for (int i = 0; i < m; ++i)
333
+ for (int j = 0; j < n; ++j)
334
+ ans += (grid2[ i] [ j ] == 1 && bfs(i, j, m, n, grid1, grid2));
335
+ return ans;
336
+ }
337
+
338
+ bool bfs(int i, int j, int m, int n, vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
339
+ queue<pair<int, int>> q;
340
+ q.push({i, j});
341
+ grid2[i][j] = 0;
342
+ bool ans = true;
343
+ vector<int> dirs = {-1, 0, 1, 0, -1};
344
+ while (!q.empty())
345
+ {
346
+ auto p = q.front();
347
+ q.pop();
348
+ i = p.first;
349
+ j = p.second;
350
+ if (grid1[i][j] == 0) ans = false;
351
+ for (int k = 0; k < 4; ++k)
352
+ {
353
+ int x = i + dirs[k], y = j + dirs[k + 1];
354
+ if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y])
355
+ {
356
+ q.push({x, y});
357
+ grid2[x][y] = 0;
358
+ }
359
+ }
360
+ }
361
+ return ans;
362
+ }
363
+ };
364
+ ```
365
+
366
+ DFS:
367
+
257
368
``` cpp
258
369
class Solution {
259
370
public:
@@ -285,6 +396,46 @@ public:
285
396
286
397
### ** Go**
287
398
399
+ BFS - Flood Fill 算法:
400
+
401
+ ``` go
402
+ func countSubIslands (grid1 [][]int , grid2 [][]int ) int {
403
+ m , n := len (grid1), len (grid1[0 ])
404
+ ans := 0
405
+ bfs := func (i, j int ) bool {
406
+ q := [][]int {{i, j}}
407
+ grid2[i][j] = 0
408
+ res := true
409
+ dirs := []int {-1 , 0 , 1 , 0 , -1 }
410
+ for len (q) > 0 {
411
+ i, j = q[0 ][0 ], q[0 ][1 ]
412
+ if grid1[i][j] == 0 {
413
+ res = false
414
+ }
415
+ q = q[1 :]
416
+ for k := 0 ; k < 4 ; k++ {
417
+ x , y := i+dirs[k], j+dirs[k+1 ]
418
+ if x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] == 1 {
419
+ q = append (q, []int {x, y})
420
+ grid2[x][y] = 0
421
+ }
422
+ }
423
+ }
424
+ return res
425
+ }
426
+ for i := 0 ; i < m; i++ {
427
+ for j := 0 ; j < n; j++ {
428
+ if grid2[i][j] == 1 && bfs (i, j) {
429
+ ans++
430
+ }
431
+ }
432
+ }
433
+ return ans
434
+ }
435
+ ```
436
+
437
+ DFS:
438
+
288
439
``` go
289
440
func countSubIslands (grid1 [][]int , grid2 [][]int ) int {
290
441
m , n := len (grid1), len (grid1[0 ])
0 commit comments