Skip to content

Commit c4eb2cb

Browse files
authored
Update 0098.所有可达路径.md
提供了Java和Python版本代码
1 parent 240684f commit c4eb2cb

File tree

1 file changed

+178
-1
lines changed

1 file changed

+178
-1
lines changed

problems/kamacoder/0098.所有可达路径.md

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,186 @@ int main() {
409409
## 其他语言版本
410410

411411
### Java
412+
#### 邻接矩阵写法
413+
```java
414+
import java.util.ArrayList;
415+
import java.util.List;
416+
import java.util.Scanner;
417+
418+
public class Main {
419+
static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径
420+
static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径
421+
422+
public static void dfs(int[][] graph, int x, int n) {
423+
// 当前遍历的节点x 到达节点n
424+
if (x == n) { // 找到符合条件的一条路径
425+
result.add(new ArrayList<>(path));
426+
return;
427+
}
428+
for (int i = 1; i <= n; i++) { // 遍历节点x链接的所有节点
429+
if (graph[x][i] == 1) { // 找到 x链接的节点
430+
path.add(i); // 遍历到的节点加入到路径中来
431+
dfs(graph, i, n); // 进入下一层递归
432+
path.remove(path.size() - 1); // 回溯,撤销本节点
433+
}
434+
}
435+
}
412436

413-
### Python
437+
public static void main(String[] args) {
438+
Scanner scanner = new Scanner(System.in);
439+
int n = scanner.nextInt();
440+
int m = scanner.nextInt();
441+
442+
// 节点编号从1到n,所以申请 n+1 这么大的数组
443+
int[][] graph = new int[n + 1][n + 1];
444+
445+
for (int i = 0; i < m; i++) {
446+
int s = scanner.nextInt();
447+
int t = scanner.nextInt();
448+
// 使用邻接矩阵表示无向图,1 表示 s 与 t 是相连的
449+
graph[s][t] = 1;
450+
}
451+
452+
path.add(1); // 无论什么路径已经是从1节点出发
453+
dfs(graph, 1, n); // 开始遍历
454+
455+
// 输出结果
456+
if (result.isEmpty()) System.out.println(-1);
457+
for (List<Integer> pa : result) {
458+
for (int i = 0; i < pa.size() - 1; i++) {
459+
System.out.print(pa.get(i) + " ");
460+
}
461+
System.out.println(pa.get(pa.size() - 1));
462+
}
463+
}
464+
}
465+
```
466+
467+
#### 邻接表写法
468+
```java
469+
import java.util.ArrayList;
470+
import java.util.LinkedList;
471+
import java.util.List;
472+
import java.util.Scanner;
473+
474+
public class Main {
475+
static List<List<Integer>> result = new ArrayList<>(); // 收集符合条件的路径
476+
static List<Integer> path = new ArrayList<>(); // 1节点到终点的路径
477+
478+
public static void dfs(List<LinkedList<Integer>> graph, int x, int n) {
479+
if (x == n) { // 找到符合条件的一条路径
480+
result.add(new ArrayList<>(path));
481+
return;
482+
}
483+
for (int i : graph.get(x)) { // 找到 x指向的节点
484+
path.add(i); // 遍历到的节点加入到路径中来
485+
dfs(graph, i, n); // 进入下一层递归
486+
path.remove(path.size() - 1); // 回溯,撤销本节点
487+
}
488+
}
489+
490+
public static void main(String[] args) {
491+
Scanner scanner = new Scanner(System.in);
492+
int n = scanner.nextInt();
493+
int m = scanner.nextInt();
494+
495+
// 节点编号从1到n,所以申请 n+1 这么大的数组
496+
List<LinkedList<Integer>> graph = new ArrayList<>(n + 1);
497+
for (int i = 0; i <= n; i++) {
498+
graph.add(new LinkedList<>());
499+
}
500+
501+
while (m-- > 0) {
502+
int s = scanner.nextInt();
503+
int t = scanner.nextInt();
504+
// 使用邻接表表示 s -> t 是相连的
505+
graph.get(s).add(t);
506+
}
414507

508+
path.add(1); // 无论什么路径已经是从1节点出发
509+
dfs(graph, 1, n); // 开始遍历
510+
511+
// 输出结果
512+
if (result.isEmpty()) System.out.println(-1);
513+
for (List<Integer> pa : result) {
514+
for (int i = 0; i < pa.size() - 1; i++) {
515+
System.out.print(pa.get(i) + " ");
516+
}
517+
System.out.println(pa.get(pa.size() - 1));
518+
}
519+
}
520+
}
521+
```
522+
### Python
523+
#### 邻接矩阵写法
524+
``` python
525+
def dfs(graph, x, n, path, result):
526+
if x == n:
527+
result.append(path.copy())
528+
return
529+
for i in range(1, n + 1):
530+
if graph[x][i] == 1:
531+
path.append(i)
532+
dfs(graph, i, n, path, result)
533+
path.pop()
534+
535+
def main():
536+
n, m = map(int, input().split())
537+
graph = [[0] * (n + 1) for _ in range(n + 1)]
538+
539+
for _ in range(m):
540+
s, t = map(int, input().split())
541+
graph[s][t] = 1
542+
543+
result = []
544+
dfs(graph, 1, n, [1], result)
545+
546+
if not result:
547+
print(-1)
548+
else:
549+
for path in result:
550+
print(' '.join(map(str, path)))
551+
552+
if __name__ == "__main__":
553+
main()
554+
```
555+
556+
#### 邻接表写法
557+
``` python
558+
from collections import defaultdict
559+
560+
result = [] # 收集符合条件的路径
561+
path = [] # 1节点到终点的路径
562+
563+
def dfs(graph, x, n):
564+
if x == n: # 找到符合条件的一条路径
565+
result.append(path.copy())
566+
return
567+
for i in graph[x]: # 找到 x指向的节点
568+
path.append(i) # 遍历到的节点加入到路径中来
569+
dfs(graph, i, n) # 进入下一层递归
570+
path.pop() # 回溯,撤销本节点
571+
572+
def main():
573+
n, m = map(int, input().split())
574+
575+
graph = defaultdict(list) # 邻接表
576+
for _ in range(m):
577+
s, t = map(int, input().split())
578+
graph[s].append(t)
579+
580+
path.append(1) # 无论什么路径已经是从1节点出发
581+
dfs(graph, 1, n) # 开始遍历
582+
583+
# 输出结果
584+
if not result:
585+
print(-1)
586+
for pa in result:
587+
print(' '.join(map(str, pa)))
588+
589+
if __name__ == "__main__":
590+
main()
591+
```
415592
### Go
416593

417594
### Rust

0 commit comments

Comments
 (0)