@@ -562,7 +562,213 @@ function pacificAtlantic(heights) {
562
562
}
563
563
```
564
564
565
+ ### go
566
+
567
+ dfs:
568
+
569
+ ``` go
570
+ var DIRECTIONS = [4 ][2 ]int {{-1 , 0 }, {1 , 0 }, {0 , -1 }, {0 , 1 }}
571
+
572
+ func pacificAtlantic (heights [][]int ) [][]int {
573
+ res := make ([][]int , 0 )
574
+ pacific := make ([][]bool , len (heights))
575
+ atlantic := make ([][]bool , len (heights))
576
+ for i := 0 ; i < len (heights); i++ {
577
+ pacific[i] = make ([]bool , len (heights[0 ]))
578
+ atlantic[i] = make ([]bool , len (heights[0 ]))
579
+ }
580
+ // 列
581
+ for i := 0 ; i < len (heights); i++ {
582
+ dfs (heights, pacific, i, 0 )
583
+ dfs (heights, atlantic, i, len (heights[0 ])-1 )
584
+ }
585
+ // 行
586
+ for j := 0 ; j < len (heights[0 ]); j++ {
587
+ dfs (heights, pacific, 0 , j)
588
+ dfs (heights, atlantic, len (heights)-1 , j)
589
+ }
590
+
591
+ for i := 0 ; i < len (heights); i++ {
592
+ for j := 0 ; j < len (heights[0 ]); j++ {
593
+ if pacific[i][j] && atlantic[i][j] {
594
+ res = append (res, []int {i, j})
595
+ }
596
+ }
597
+ }
598
+
599
+ return res
600
+ }
601
+
602
+ func dfs (heights [][]int , visited [][]bool , i , j int ) {
603
+ visited[i][j] = true
604
+ for _ , d := range DIRECTIONS {
605
+ x , y := i+d[0 ], j+d[1 ]
606
+ if x < 0 || x >= len (heights) || y < 0 || y >= len (heights[0 ]) || heights[i][j] > heights[x][y] || visited[x][y] {
607
+ continue
608
+ }
609
+
610
+ dfs (heights, visited, x, y)
611
+ }
612
+ }
613
+ ```
614
+
615
+ bfs:
616
+
617
+ ``` go
618
+ var DIRECTIONS = [4 ][2 ]int {{-1 , 0 }, {1 , 0 }, {0 , -1 }, {0 , 1 }}
619
+
620
+ func pacificAtlantic (heights [][]int ) [][]int {
621
+ res := make ([][]int , 0 )
622
+ pacific := make ([][]bool , len (heights))
623
+ atlantic := make ([][]bool , len (heights))
624
+ for i := 0 ; i < len (heights); i++ {
625
+ pacific[i] = make ([]bool , len (heights[0 ]))
626
+ atlantic[i] = make ([]bool , len (heights[0 ]))
627
+ }
628
+ // 列
629
+ for i := 0 ; i < len (heights); i++ {
630
+ bfs (heights, pacific, i, 0 )
631
+ bfs (heights, atlantic, i, len (heights[0 ])-1 )
632
+ }
633
+ // 行
634
+ for j := 0 ; j < len (heights[0 ]); j++ {
635
+ bfs (heights, pacific, 0 , j)
636
+ bfs (heights, atlantic, len (heights)-1 , j)
637
+ }
638
+
639
+ for i := 0 ; i < len (heights); i++ {
640
+ for j := 0 ; j < len (heights[0 ]); j++ {
641
+ if pacific[i][j] && atlantic[i][j] {
642
+ res = append (res, []int {i, j})
643
+ }
644
+ }
645
+ }
646
+
647
+ return res
648
+ }
649
+
650
+ func bfs (heights [][]int , visited [][]bool , i , j int ) {
651
+ queue := make ([][]int , 0 )
652
+ queue = append (queue, []int {i, j})
653
+ visited[i][j] = true
654
+ for len (queue) > 0 {
655
+ cur := queue[0 ]
656
+ queue = queue[1 :]
657
+ for _ , d := range DIRECTIONS {
658
+ x , y := cur[0 ]+d[0 ], cur[1 ]+d[1 ]
659
+ if x < 0 || x >= len (heights) || y < 0 || y >= len (heights[0 ]) || heights[cur[0 ]][cur[1 ]] > heights[x][y] || visited[x][y] {
660
+ continue
661
+ }
662
+ queue = append (queue, []int {x, y})
663
+ visited[x][y] = true
664
+ }
665
+ }
666
+ }
667
+ ```
668
+
669
+ ### Rust
670
+
671
+ dfs:
565
672
673
+ ``` rust
674
+ impl Solution {
675
+ const DIRECTIONS : [(isize , isize ); 4 ] = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 )];
676
+ pub fn pacific_atlantic (heights : Vec <Vec <i32 >>) -> Vec <Vec <i32 >> {
677
+ let (m , n ) = (heights . len (), heights [0 ]. len ());
678
+ let mut res = vec! [];
679
+ let (mut pacific , mut atlantic ) = (vec! [vec! [false ; n ]; m ], vec! [vec! [false ; n ]; m ]);
680
+
681
+ // 列
682
+ for i in 0 .. m {
683
+ Self :: dfs (& heights , & mut pacific , i , 0 );
684
+ Self :: dfs (& heights , & mut atlantic , i , n - 1 );
685
+ }
686
+
687
+ for j in 0 .. n {
688
+ Self :: dfs (& heights , & mut pacific , 0 , j );
689
+ Self :: dfs (& heights , & mut atlantic , m - 1 , j );
690
+ }
691
+
692
+ for i in 0 .. m {
693
+ for j in 0 .. n {
694
+ if pacific [i ][j ] && atlantic [i ][j ] {
695
+ res . push (vec! [i as i32 , j as i32 ]);
696
+ }
697
+ }
698
+ }
699
+
700
+ res
701
+ }
702
+
703
+ pub fn dfs (heights : & [Vec <i32 >], visited : & mut [Vec <bool >], i : usize , j : usize ) {
704
+ visited [i ][j ] = true ;
705
+ for (dx , dy ) in Self :: DIRECTIONS {
706
+ let (x , y ) = (i as isize + dx , j as isize + dy );
707
+ if x < 0 || x >= heights . len () as isize || y < 0 || y >= heights [0 ]. len () as isize {
708
+ continue ;
709
+ }
710
+ let (x , y ) = (x as usize , y as usize );
711
+ if ! visited [x ][y ] && heights [x ][y ] >= heights [i ][j ] {
712
+ Self :: dfs (heights , visited , x , y );
713
+ }
714
+ }
715
+ }
716
+ }
717
+ ```
718
+
719
+ bfs:
720
+
721
+ ``` rust
722
+ use std :: collections :: VecDeque ;
723
+
724
+ impl Solution {
725
+ const DIRECTIONS : [(isize , isize ); 4 ] = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 )];
726
+ pub fn pacific_atlantic (heights : Vec <Vec <i32 >>) -> Vec <Vec <i32 >> {
727
+ let (m , n ) = (heights . len (), heights [0 ]. len ());
728
+ let mut res = vec! [];
729
+ let (mut pacific , mut atlantic ) = (vec! [vec! [false ; n ]; m ], vec! [vec! [false ; n ]; m ]);
730
+
731
+ // 列
732
+ for i in 0 .. m {
733
+ Self :: bfs (& heights , & mut pacific , i , 0 );
734
+ Self :: bfs (& heights , & mut atlantic , i , n - 1 );
735
+ }
736
+
737
+ for j in 0 .. n {
738
+ Self :: bfs (& heights , & mut pacific , 0 , j );
739
+ Self :: bfs (& heights , & mut atlantic , m - 1 , j );
740
+ }
741
+
742
+ for i in 0 .. m {
743
+ for j in 0 .. n {
744
+ if pacific [i ][j ] && atlantic [i ][j ] {
745
+ res . push (vec! [i as i32 , j as i32 ]);
746
+ }
747
+ }
748
+ }
749
+
750
+ res
751
+ }
752
+
753
+ pub fn bfs (heights : & [Vec <i32 >], visited : & mut [Vec <bool >], i : usize , j : usize ) {
754
+ let mut queue = VecDeque :: from ([(i , j )]);
755
+ visited [i ][j ] = true ;
756
+ while let Some ((i , j )) = queue . pop_front () {
757
+ for (dx , dy ) in Self :: DIRECTIONS {
758
+ let (x , y ) = (i as isize + dx , j as isize + dy );
759
+ if x < 0 || x >= heights . len () as isize || y < 0 || y >= heights [0 ]. len () as isize {
760
+ continue ;
761
+ }
762
+ let (x , y ) = (x as usize , y as usize );
763
+ if ! visited [x ][y ] && heights [x ][y ] >= heights [i ][j ] {
764
+ queue . push_back ((x , y ));
765
+ visited [x ][y ] = true ;
766
+ }
767
+ }
768
+ }
769
+ }
770
+ }
771
+ ```
566
772
567
773
<p align =" center " >
568
774
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
0 commit comments