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

[pull] master from youngyangyang04:master #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 3 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
添加 0103.水流问题. Go语言 DFS版本
  • Loading branch information
catherinexrk authored Nov 12, 2024
commit 947424d311d767dec59f5820b9a83f51da5c2bcc
75 changes: 75 additions & 0 deletions problems/kamacoder/0103.水流问题.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,81 @@ if __name__ == "__main__":
```

### Go
```go
package main

import (
"os"
"fmt"
"strings"
"strconv"
"bufio"
)

var directions = [][]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} // 四个方向的偏移量

func main() {
scanner := bufio.NewScanner(os.Stdin)

scanner.Scan()
lineList := strings.Fields(scanner.Text())
N, _ := strconv.Atoi(lineList[0])
M, _ := strconv.Atoi(lineList[1])

grid := make([][]int, N)
visited := make([][]bool, N) // 用于标记是否访问过
for i := 0; i < N; i++ {
grid[i] = make([]int, M)
visited[i] = make([]bool, M)
scanner.Scan()
lineList = strings.Fields(scanner.Text())

for j := 0; j < M; j++ {
grid[i][j], _ = strconv.Atoi(lineList[j])
}
}

// 遍历每个单元格,使用DFS检查是否可达两组边界
for i := 0; i < N; i++ {
for j := 0; j < M; j++ {
canReachFirst, canReachSecond := dfs(grid, visited, i, j)
if canReachFirst && canReachSecond {
fmt.Println(strconv.Itoa(i) + " " + strconv.Itoa(j))
}
}
}
}

func dfs(grid [][]int, visited [][]bool, startx int, starty int) (bool, bool) {
visited[startx][starty] = true
canReachFirst := startx == 0 || starty == 0 || startx == len(grid)-1 || starty == len(grid[0])-1
canReachSecond := startx == len(grid)-1 || starty == len(grid[0])-1 || startx == 0 || starty == 0

if canReachFirst && canReachSecond {
return true, true
}

for _, direction := range directions {
nextx := startx + direction[0]
nexty := starty + direction[1]

if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
continue
}

if grid[nextx][nexty] <= grid[startx][starty] && !visited[nextx][nexty] {
hasReachFirst, hasReachSecond := dfs(grid, visited, nextx, nexty)
if !canReachFirst {
canReachFirst = hasReachFirst
}
if !canReachSecond {
canReachSecond = hasReachSecond
}
}
}
return canReachFirst, canReachSecond
}
```

### Rust

Expand Down

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