Skip to content

Commit c6b54b1

Browse files
Merge pull request #2622 from nineninee/km0098
添加 卡码网0098.所有可达路径 JS版
2 parents 0cf122e + 7aa230e commit c6b54b1

File tree

1 file changed

+142
-4
lines changed

1 file changed

+142
-4
lines changed

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

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
## 插曲
7777

78-
-------------
78+
-------------
7979

8080
本题和力扣 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) 是一样的,录友了解深度优先搜索之后,这道题目就是模板题,是送分题。
8181

@@ -475,7 +475,7 @@ public class Main {
475475
}
476476
}
477477
}
478-
```
478+
```
479479

480480
#### 邻接表写法
481481
```java
@@ -564,7 +564,7 @@ def main():
564564

565565
if __name__ == "__main__":
566566
main()
567-
```
567+
```
568568

569569
#### 邻接表写法
570570
``` python
@@ -608,6 +608,145 @@ if __name__ == "__main__":
608608

609609
### Javascript
610610

611+
#### 邻接矩阵写法
612+
613+
```javascript
614+
const r1 = require('readline').createInterface({ input: process.stdin });
615+
// 创建readline接口
616+
let iter = r1[Symbol.asyncIterator]();
617+
// 创建异步迭代器
618+
const readline = async ()=>(await iter.next()).value;
619+
620+
621+
let graph;
622+
let N, M;
623+
// 收集符合条件的路径
624+
let result = [];
625+
// 1节点到终点的路径
626+
let path = [];
627+
628+
// 创建邻接矩阵,初始化邻接矩阵
629+
async function initGraph(){
630+
let line;
631+
632+
line = await readline();
633+
[N, M] = line.split(' ').map(i => parseInt(i))
634+
graph = new Array(N + 1).fill(0).map(() => new Array(N + 1).fill(0))
635+
636+
while(M--){
637+
line = await readline()
638+
const strArr = line ? line.split(' ').map(i => parseInt(i)) : undefined
639+
strArr ? graph[strArr[0]][strArr[1]] = 1 : null
640+
}
641+
};
642+
643+
// 深度搜索
644+
function dfs(graph, x, n){
645+
// 当前遍历节点为x, 到达节点为n
646+
if(x == n){
647+
result.push([...path])
648+
return
649+
}
650+
for(let i = 1 ; i <= n ; i++){
651+
if(graph[x][i] == 1){
652+
path.push(i)
653+
dfs(graph, i, n )
654+
path.pop(i)
655+
}
656+
}
657+
};
658+
659+
(async function(){
660+
// 创建邻接矩阵,初始化邻接矩阵
661+
await initGraph();
662+
663+
// 从节点1开始深度搜索
664+
path.push(1);
665+
666+
// 深度搜索
667+
dfs(graph, 1, N );
668+
669+
// 输出
670+
if(result.length > 0){
671+
result.forEach(i => {
672+
console.log(i.join(' '))
673+
})
674+
}else{
675+
console.log(-1)
676+
}
677+
678+
})();
679+
680+
```
681+
682+
683+
684+
#### 邻接表写法
685+
686+
```javascript
687+
const r1 = require('readline').createInterface({ input: process.stdin });
688+
// 创建readline接口
689+
let iter = r1[Symbol.asyncIterator]();
690+
// 创建异步迭代器
691+
const readline = async () => (await iter.next()).value;
692+
693+
let graph;
694+
let N, M;
695+
696+
// 收集符合条件的路径
697+
let result = [];
698+
// 1节点到终点的路径
699+
let path = [];
700+
701+
// 创建邻接表,初始化邻接表
702+
async function initGraph() {
703+
let line;
704+
line = await readline();
705+
[N, M] = line.split(' ').map(i => parseInt(i))
706+
graph = new Array(N + 1).fill(0).map(() => new Array())
707+
708+
while (line = await readline()) {
709+
const strArr = line.split(' ').map(i => parseInt(i))
710+
strArr ? graph[strArr[0]].push(strArr[1]) : null
711+
}
712+
};
713+
714+
// 深度搜索
715+
async function dfs(graph, x, n) {
716+
// 当前遍历节点为x, 到达节点为n
717+
if (x == n) {
718+
result.push([...path])
719+
return
720+
}
721+
722+
graph[x].forEach(i => {
723+
path.push(i)
724+
dfs(graph, i, n)
725+
path.pop(i)
726+
})
727+
};
728+
729+
(async function () {
730+
// 创建邻接表,初始化邻接表
731+
await initGraph();
732+
733+
// 从节点1开始深度搜索
734+
path.push(1);
735+
736+
// 深度搜索
737+
dfs(graph, 1, N);
738+
739+
// 输出
740+
if (result.length > 0) {
741+
result.forEach(i => {
742+
console.log(i.join(' '))
743+
})
744+
} else {
745+
console.log(-1)
746+
}
747+
})();
748+
```
749+
611750
### TypeScript
612751

613752
### PhP
@@ -628,4 +767,3 @@ if __name__ == "__main__":
628767

629768

630769

631-

0 commit comments

Comments
 (0)