Skip to content

Commit fab781e

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 5db796f + 83e8442 commit fab781e

File tree

4 files changed

+299
-5
lines changed

4 files changed

+299
-5
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-

problems/kamacoder/0099.岛屿的数量广搜.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,87 @@ int main() {
198198

199199
### Javascript
200200

201+
```javascript
202+
const r1 = require('readline').createInterface({ input: process.stdin });
203+
// 创建readline接口
204+
let iter = r1[Symbol.asyncIterator]();
205+
// 创建异步迭代器
206+
const readline = async () => (await iter.next()).value;
207+
208+
let graph
209+
let N, M
210+
let visited
211+
let result = 0
212+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
213+
214+
// 读取输入,初始化地图
215+
const initGraph = async () => {
216+
let line = await readline();
217+
[N, M] = line.split(' ').map(Number);
218+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
219+
visited = new Array(N).fill(false).map(() => new Array(M).fill(false))
220+
221+
for (let i = 0; i < N; i++) {
222+
line = await readline()
223+
line = line.split(' ').map(Number)
224+
for (let j = 0; j < M; j++) {
225+
graph[i][j] = line[j]
226+
}
227+
}
228+
}
229+
230+
231+
/**
232+
* @description: 从(x, y)开始广度优先遍历
233+
* @param {*} graph 地图
234+
* @param {*} visited 访问过的节点
235+
* @param {*} x 开始搜索节点的下标
236+
* @param {*} y 开始搜索节点的下标
237+
* @return {*}
238+
*/
239+
const bfs = (graph, visited, x, y) => {
240+
let queue = []
241+
queue.push([x, y])
242+
visited[x][y] = true //只要加入队列就立刻标记为访问过
243+
244+
while (queue.length) {
245+
let [x, y] = queue.shift()
246+
for (let i = 0; i < 4; i++) {
247+
let nextx = x + dir[i][0]
248+
let nexty = y + dir[i][1]
249+
if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
250+
if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){
251+
queue.push([nextx, nexty])
252+
visited[nextx][nexty] = true
253+
}
254+
}
255+
}
256+
257+
}
258+
259+
(async function () {
260+
261+
// 读取输入,初始化地图
262+
await initGraph()
263+
264+
// 统计岛屿数
265+
for (let i = 0; i < N; i++) {
266+
for (let j = 0; j < M; j++) {
267+
if (!visited[i][j] && graph[i][j] === 1) {
268+
// 遇到没访问过的陆地,+1
269+
result++
270+
271+
// 广度优先遍历,将相邻陆地标记为已访问
272+
bfs(graph, visited, i, j)
273+
}
274+
}
275+
}
276+
console.log(result);
277+
})()
278+
```
279+
280+
281+
201282
### TypeScript
202283

203284
### PhP

problems/kamacoder/0099.岛屿的数量深搜.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,81 @@ if __name__ == '__main__':
285285

286286
### Javascript
287287

288+
```javascript
289+
const r1 = require('readline').createInterface({ input: process.stdin });
290+
// 创建readline接口
291+
let iter = r1[Symbol.asyncIterator]();
292+
// 创建异步迭代器
293+
const readline = async () => (await iter.next()).value;
294+
295+
let graph
296+
let N, M
297+
let visited
298+
let result = 0
299+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
300+
301+
// 读取输入,初始化地图
302+
const initGraph = async () => {
303+
let line = await readline();
304+
[N, M] = line.split(' ').map(Number);
305+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
306+
visited = new Array(N).fill(false).map(() => new Array(M).fill(false))
307+
308+
for (let i = 0; i < N; i++) {
309+
line = await readline()
310+
line = line.split(' ').map(Number)
311+
for (let j = 0; j < M; j++) {
312+
graph[i][j] = line[j]
313+
}
314+
}
315+
}
316+
317+
/**
318+
* @description: 从节点x,y开始深度优先遍历
319+
* @param {*} graph 是地图,也就是一个二维数组
320+
* @param {*} visited 标记访问过的节点,不要重复访问
321+
* @param {*} x 表示开始搜索节点的下标
322+
* @param {*} y 表示开始搜索节点的下标
323+
* @return {*}
324+
*/
325+
const dfs = (graph, visited, x, y) => {
326+
for (let i = 0; i < 4; i++) {
327+
const nextx = x + dir[i][0]
328+
const nexty = y + dir[i][1]
329+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
330+
if (!visited[nextx][nexty] && graph[nextx][nexty] === 1) {
331+
visited[nextx][nexty] = true
332+
dfs(graph, visited, nextx, nexty)
333+
}
334+
}
335+
}
336+
337+
(async function () {
338+
339+
// 读取输入,初始化地图
340+
await initGraph()
341+
342+
// 统计岛屿数
343+
for (let i = 0; i < N; i++) {
344+
for (let j = 0; j < M; j++) {
345+
if (!visited[i][j] && graph[i][j] === 1) {
346+
// 标记已访问
347+
visited[i][j] = true
348+
349+
// 遇到没访问过的陆地,+1
350+
result++
351+
352+
// 深度优先遍历,将相邻陆地标记为已访问
353+
dfs(graph, visited, i, j)
354+
}
355+
}
356+
}
357+
console.log(result);
358+
})()
359+
```
360+
361+
362+
288363
### TypeScript
289364

290365
### PhP

problems/kamacoder/0106.岛屿的周长.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png)
6868

69-
该录友的下边空格出界了,则说明找到一条边。
69+
该陆地的下边空格出界了,则说明找到一条边。
7070

7171

7272
C++代码如下:(详细注释)

0 commit comments

Comments
 (0)