Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b19b832

Browse files
authored
Update 0417.太平洋大西洋水流问题.md
1 parent 9928779 commit b19b832

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

‎problems/0417.太平洋大西洋水流问题.md‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,109 @@ function pacificAtlantic(heights) {
562562
}
563563
```
564564

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+
```
565614

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+
```
566668

567669
<p align="center">
568670
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /