@@ -190,59 +190,163 @@ int main() {
190
190
191
191
### Java
192
192
193
+ ``` java
194
+
195
+ import java.util.Scanner ;
196
+
197
+ public class Main {
198
+ static int [][] dir = { {0 , 1 }, {1 , 0 }, {- 1 , 0 }, {0 , - 1 } }; // 四个方向
199
+
200
+ public static void dfs (int [][] grid , boolean [][] visited , int x , int y ) {
201
+ for (int i = 0 ; i < 4 ; i++ ) {
202
+ int nextx = x + dir[i][0 ];
203
+ int nexty = y + dir[i][1 ];
204
+ if (nextx < 0 || nextx >= grid. length || nexty < 0 || nexty >= grid[0 ]. length) continue ; // 越界了,直接跳过
205
+ if (! visited[nextx][nexty] && grid[nextx][nexty] == 1 ) { // 没有访问过的 同时 是陆地的
206
+ visited[nextx][nexty] = true ;
207
+ dfs(grid, visited, nextx, nexty);
208
+ }
209
+ }
210
+ }
211
+
212
+ public static void main (String [] args ) {
213
+ Scanner scanner = new Scanner (System . in);
214
+ int n = scanner. nextInt();
215
+ int m = scanner. nextInt();
216
+ int [][] grid = new int [n][m];
217
+ for (int i = 0 ; i < n; i++ ) {
218
+ for (int j = 0 ; j < m; j++ ) {
219
+ grid[i][j] = scanner. nextInt();
220
+ }
221
+ }
222
+
223
+ boolean [][] visited = new boolean [n][m];
224
+
225
+ int result = 0 ;
226
+ for (int i = 0 ; i < n; i++ ) {
227
+ for (int j = 0 ; j < m; j++ ) {
228
+ if (! visited[i][j] && grid[i][j] == 1 ) {
229
+ visited[i][j] = true ;
230
+ result++ ; // 遇到没访问过的陆地,+1
231
+ dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
232
+ }
233
+ }
234
+ }
235
+
236
+ System . out. println(result);
237
+ }
238
+ }
239
+
240
+
241
+
242
+ ```
243
+
244
+
193
245
### Python
194
246
195
247
``` python
196
- from collections import deque
197
248
198
- # 四个方向
199
- position = [[0 , 1 ], [1 , 0 ], [0 , - 1 ], [- 1 , 0 ]]
249
+ def dfs (grid , visited , x , y ):
250
+ dir = [(0 , 1 ), (1 , 0 ), (- 1 , 0 ), (0 , - 1 )] # 四个方向
251
+ for d in dir :
252
+ nextx, nexty = x + d[0 ], y + d[1 ]
253
+ if 0 <= nextx < len (grid) and 0 <= nexty < len (grid[0 ]):
254
+ if not visited[nextx][nexty] and grid[nextx][nexty] == 1 : # 没有访问过的 同时 是陆地的
255
+ visited[nextx][nexty] = True
256
+ dfs(grid, visited, nextx, nexty)
257
+
258
+ def main ():
259
+ n, m = map (int , input ().split())
260
+ grid = [list (map (int , input ().split())) for _ in range (n)]
261
+ visited = [[False ] * m for _ in range (n)]
262
+
263
+ result = 0
264
+ for i in range (n):
265
+ for j in range (m):
266
+ if not visited[i][j] and grid[i][j] == 1 :
267
+ visited[i][j] = True
268
+ result += 1 # 遇到没访问过的陆地,+1
269
+ dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True
270
+
271
+ print (result)
272
+
273
+ if __name__ == " __main__" :
274
+ main()
275
+
276
+
277
+
278
+ ```
279
+
280
+
281
+ ### Go
282
+
283
+ ``` go
200
284
285
+ package main
201
286
202
- def bfs (grid , visited , x , y ):
203
- """
204
- 广度优先搜索对陆地进行标记
205
- """
287
+ import (
288
+ " bufio"
289
+ " fmt"
290
+ " os"
291
+ " strconv"
292
+ " strings"
293
+ )
206
294
207
- que = deque() # 创建队列
295
+ var dir = [ 4 ][ 2 ] int {{ 0 , 1 }, { 1 , 0 }, {- 1 , 0 }, { 0 , - 1 }} // 四个方向
208
296
209
- # 标记当前节点并加入队列
210
- visited[x][y] = True
211
- que.append([x, y])
297
+ func dfs (grid [][]int , visited [][]bool , x , y int ) {
298
+ for i := 0 ; i < 4 ; i++ {
299
+ nextx := x + dir[i][0 ]
300
+ nexty := y + dir[i][1 ]
301
+ if nextx < 0 || nextx >= len (grid) || nexty < 0 || nexty >= len (grid[0 ]) {
302
+ continue // 越界了,直接跳过
303
+ }
304
+ if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的
305
+ visited[nextx][nexty] = true
306
+ dfs (grid, visited, nextx, nexty)
307
+ }
308
+ }
309
+ }
212
310
213
- while que:
214
- cur_x, cur_y = que.popleft() # 取出队首节点
215
- for i, j in position:
216
- next_x = cur_x + i
217
- next_y = cur_y + j
218
- # 下一节点下标越界,跳过
219
- if next_x < 0 or next_x >= len (grid) or next_y < 0 or next_y >= len (grid[0 ]):
220
- continue
221
- # 下一节点是陆地且未被访问,标记节点并加入队列
222
- if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
223
- visited[next_x][next_y] = True
224
- que.append([next_x, next_y])
311
+ func main () {
312
+ reader := bufio.NewReader (os.Stdin )
313
+ var n , m int
314
+ fmt.Scanf (" %d %d " , &n, &m)
315
+
316
+ grid := make ([][]int , n)
317
+ for i := 0 ; i < n; i++ {
318
+ grid[i] = make ([]int , m)
319
+ line , _ := reader.ReadString (' \n ' )
320
+ line = strings.TrimSpace (line)
321
+ elements := strings.Split (line, " " )
322
+ for j := 0 ; j < m; j++ {
323
+ grid[i][j], _ = strconv.Atoi (elements[j])
324
+ }
325
+ }
225
326
327
+ visited := make ([][]bool , n)
328
+ for i := 0 ; i < n; i++ {
329
+ visited[i] = make ([]bool , m)
330
+ }
226
331
227
- n, m = map (int , input ().split())
228
- # 邻接矩阵
229
- grid = []
230
- for i in range (n):
231
- grid.append(list (map (int , input ().split())))
332
+ result := 0
333
+ for i := 0 ; i < n; i++ {
334
+ for j := 0 ; j < m; j++ {
335
+ if !visited[i][j] && grid[i][j] == 1 {
336
+ visited[i][j] = true
337
+ result++ // 遇到没访问过的陆地,+1
338
+ dfs (grid, visited, i, j) // 将与其链接的陆地都标记上 true
339
+ }
340
+ }
341
+ }
232
342
233
- visited = [[False ] * m for _ in range (n)] # 访问表
343
+ fmt.Println (result)
344
+ }
234
345
235
- res = 0
236
- for i in range (n):
237
- for j in range (m):
238
- if grid[i][j] == 1 and not visited[i][j]:
239
- res += 1
240
- bfs(grid, visited, i, j)
241
346
242
- print (res)
243
347
```
244
348
245
- ### Go
349
+
246
350
247
351
### Rust
248
352
@@ -333,12 +437,62 @@ const bfs = (graph, visited, x, y) => {
333
437
334
438
### PhP
335
439
440
+ ``` PHP
441
+
442
+ <?php
443
+
444
+ function dfs($grid, & $visited, $x, $y) {
445
+ $dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
446
+ foreach ($dir as $d) {
447
+ $nextx = $x + $d[0];
448
+ $nexty = $y + $d[1];
449
+ if ($nextx < 0 || $nextx >= count($grid) || $nexty < 0 || $nexty >= count($grid[0])) {
450
+ continue; // 越界了,直接跳过
451
+ }
452
+ if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的
453
+ $visited[$nextx][$nexty] = true;
454
+ dfs($grid, $visited, $nextx, $nexty);
455
+ }
456
+ }
457
+ }
458
+
459
+ function main() {
460
+ fscanf(STDIN, "%d %d", $n, $m);
461
+ $grid = [];
462
+ for ($i = 0; $i < $n; $i++) {
463
+ $grid[$i] = array_map('intval', explode(' ', trim(fgets(STDIN))));
464
+ }
465
+
466
+ $visited = array_fill(0, $n, array_fill(0, $m, false));
467
+
468
+ $result = 0;
469
+ for ($i = 0; $i < $n; $i++) {
470
+ for ($j = 0; $j < $m; $j++) {
471
+ if (!$visited[$i][$j] && $grid[$i][$j] == 1) {
472
+ $visited[$i][$j] = true;
473
+ $result++; // 遇到没访问过的陆地,+1
474
+ dfs($grid, $visited, $i, $j); // 将与其链接的陆地都标记上 true
475
+ }
476
+ }
477
+ }
478
+
479
+ echo $result . PHP_EOL;
480
+ }
481
+
482
+ main();
483
+ ?>
484
+
485
+
486
+ ```
487
+
488
+
336
489
### Swift
337
490
338
491
### Scala
339
492
340
493
### C#
341
494
495
+
342
496
### Dart
343
497
344
498
### C
0 commit comments