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 #469

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 8 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 29, 2024
Merged
Changes from 2 commits
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
119 changes: 119 additions & 0 deletions problems/kamacoder/0098.所有可达路径.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,125 @@ if __name__ == "__main__":
```
### Go

#### 邻接矩阵写法
```go
package main

import (
"fmt"
)

var result [][]int // 收集符合条件的路径
var path []int // 1节点到终点的路径

func dfs(graph [][]int, x, n int) {
// 当前遍历的节点x 到达节点n
if x == n { // 找到符合条件的一条路径
temp := make([]int, len(path))
copy(temp, path)
result = append(result, temp)
return
}
for i := 1; i <= n; i++ { // 遍历节点x链接的所有节点
if graph[x][i] == 1 { // 找到 x链接的节点
path = append(path, i) // 遍历到的节点加入到路径中来
dfs(graph, i, n) // 进入下一层递归
path = path[:len(path)-1] // 回溯,撤销本节点
}
}
}

func main() {
var n, m int
fmt.Scanf("%d %d", &n, &m)

// 节点编号从1到n,所以申请 n+1 这么大的数组
graph := make([][]int, n+1)
for i := range graph {
graph[i] = make([]int, n+1)
}

for i := 0; i < m; i++ {
var s, t int
fmt.Scanf("%d %d", &s, &t)
// 使用邻接矩阵表示无向图,1 表示 s 与 t 是相连的
graph[s][t] = 1
}

path = append(path, 1) // 无论什么路径已经是从1节点出发
dfs(graph, 1, n) // 开始遍历

// 输出结果
if len(result) == 0 {
fmt.Println(-1)
} else {
for _, pa := range result {
for i := 0; i < len(pa)-1; i++ {
fmt.Print(pa[i], " ")
}
fmt.Println(pa[len(pa)-1])
}
}
}
```

#### 邻接表写法
```go
package main

import (
"fmt"
)

var result [][]int
var path []int

func dfs(graph [][]int, x, n int) {
if x == n {
temp := make([]int, len(path))
copy(temp, path)
result = append(result, temp)
return
}
for _, i := range graph[x] {
path = append(path, i)
dfs(graph, i, n)
path = path[:len(path)-1]
}
}

func main() {
var n, m int
fmt.Scanf("%d %d", &n, &m)

graph := make([][]int, n+1)
for i := 0; i <= n; i++ {
graph[i] = make([]int, 0)
}

for m > 0 {
var s, t int
fmt.Scanf("%d %d", &s, &t)
graph[s] = append(graph[s], t)
m--
}

path = append(path, 1)
dfs(graph, 1, n)

if len(result) == 0 {
fmt.Println(-1)
} else {
for _, pa := range result {
for i := 0; i < len(pa)-1; i++ {
fmt.Print(pa[i], " ")
}
fmt.Println(pa[len(pa)-1])
}
}
}
```

### Rust

### Javascript
Expand Down

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