@@ -428,6 +428,146 @@ var maxAreaOfIsland = function (grid) {
428
428
};
429
429
```
430
430
431
+ ### Go
432
+
433
+ dsf: 版本一
434
+
435
+ ``` go
436
+ var DIRECTIONS = [4 ][2 ]int {{-1 , 0 }, {0 , -1 }, {1 , 0 }, {0 , 1 }}
437
+ var count int = 0
438
+
439
+ func maxAreaOfIsland (grid [][]int ) int {
440
+ res := 0
441
+ visited := make ([][]bool , len (grid))
442
+ for i := 0 ; i < len (grid); i++ {
443
+ visited[i] = make ([]bool , len (grid[0 ]))
444
+ }
445
+
446
+ for i , rows := range grid {
447
+ for j , v := range rows {
448
+ if v == 1 && !visited[i][j] {
449
+ // 第一种写法,重制 count,必定有 1 个
450
+ count = 1
451
+ dfs (grid, visited, i, j)
452
+ res = max (res, count)
453
+ }
454
+
455
+ }
456
+ }
457
+
458
+ return res
459
+ }
460
+
461
+ // 第一种写法
462
+ func dfs (grid [][]int , visited [][]bool , i , j int ) {
463
+ visited[i][j] = true // 标记已访问,循环中未标记会导致重复
464
+ for _ , d := range DIRECTIONS {
465
+ x , y := i+d[0 ], j+d[1 ]
466
+ if x < 0 || x >= len (grid) || y < 0 || y >= len (grid[0 ]) {
467
+ continue
468
+ }
469
+ if grid[x][y] == 1 && !visited[x][y] {
470
+ count++
471
+ dfs (grid, visited, x, y)
472
+ }
473
+ }
474
+ }
475
+ ```
476
+
477
+ dfs:版本二
478
+
479
+ ``` go
480
+ var DIRECTIONS = [4 ][2 ]int {{-1 , 0 }, {0 , -1 }, {1 , 0 }, {0 , 1 }}
481
+ var count int = 0
482
+
483
+ func maxAreaOfIsland (grid [][]int ) int {
484
+ res := 0
485
+ visited := make ([][]bool , len (grid))
486
+ for i := 0 ; i < len (grid); i++ {
487
+ visited[i] = make ([]bool , len (grid[0 ]))
488
+ }
489
+
490
+ for i , rows := range grid {
491
+ for j , v := range rows {
492
+ if v == 1 && !visited[i][j] {
493
+ // 第二种写法
494
+ count = 0
495
+ dfs (grid, visited, i, j)
496
+ res = max (res, count)
497
+ }
498
+
499
+ }
500
+ }
501
+
502
+ return res
503
+ }
504
+
505
+ // 第二种写法
506
+ func dfs (grid [][]int , visited [][]bool , i , j int ) {
507
+ if visited[i][j] || grid[i][j] == 0 {
508
+ return
509
+ }
510
+ visited[i][j] = true
511
+ count++
512
+ for _ , d := range DIRECTIONS {
513
+ x , y := i+d[0 ], j+d[1 ]
514
+ if x < 0 || x >= len (grid) || y < 0 || y >= len (grid[0 ]) {
515
+ continue
516
+ }
517
+ dfs (grid, visited, x, y)
518
+ }
519
+ }
520
+ ```
521
+
522
+ bfs:
523
+
524
+ ``` go
525
+ var DIRECTIONS = [4 ][2 ]int {{-1 , 0 }, {0 , -1 }, {1 , 0 }, {0 , 1 }}
526
+ var count int = 0
527
+
528
+ func maxAreaOfIsland (grid [][]int ) int {
529
+ res := 0
530
+ visited := make ([][]bool , len (grid))
531
+ for i := 0 ; i < len (grid); i++ {
532
+ visited[i] = make ([]bool , len (grid[0 ]))
533
+ }
534
+
535
+ for i , rows := range grid {
536
+ for j , v := range rows {
537
+ if v == 1 && !visited[i][j] {
538
+ count = 0
539
+ bfs (grid, visited, i, j)
540
+ res = max (res, count)
541
+ }
542
+
543
+ }
544
+ }
545
+
546
+ return res
547
+ }
548
+
549
+ // bfs
550
+ func bfs (grid [][]int , visited [][]bool , i , j int ) {
551
+ visited[i][j] = true
552
+ count++
553
+ queue := [][2 ]int {{i, j}}
554
+ for len (queue) > 0 {
555
+ cur := queue[0 ]
556
+ queue = queue[1 :]
557
+ for _ , d := range DIRECTIONS {
558
+ x , y := cur[0 ]+d[0 ], cur[1 ]+d[1 ]
559
+ if x < 0 || x >= len (grid) || y < 0 || y >= len (grid[0 ]) {
560
+ continue
561
+ }
562
+ if grid[x][y] == 1 && !visited[x][y] {
563
+ count++
564
+ queue = append (queue, [2 ]int {x, y})
565
+ visited[x][y] = true
566
+ }
567
+ }
568
+ }
569
+ }
570
+ ```
431
571
432
572
### Rust
433
573
0 commit comments