diff --git a/problems/kamacoder/0098.所有可达路径.md b/problems/kamacoder/0098.所有可达路径.md index 073d1a2e..3281303d 100644 --- a/problems/kamacoder/0098.所有可达路径.md +++ b/problems/kamacoder/0098.所有可达路径.md @@ -75,7 +75,7 @@ ## 插曲 -------------- +------------- 本题和力扣 [797.所有可能的路径](https://leetcode.cn/problems/all-paths-from-source-to-target/description/) 是一样的,录友了解深度优先搜索之后,这道题目就是模板题,是送分题。 @@ -475,7 +475,7 @@ public class Main { } } } -``` +``` #### 邻接表写法 ```java @@ -564,7 +564,7 @@ def main(): if __name__ == "__main__": main() -``` +``` #### 邻接表写法 ``` python @@ -608,6 +608,145 @@ if __name__ == "__main__": ### Javascript +#### 邻接矩阵写法 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async ()=>(await iter.next()).value; + + +let graph; +let N, M; +// 收集符合条件的路径 +let result = []; +// 1节点到终点的路径 +let path = []; + +// 创建邻接矩阵,初始化邻接矩阵 +async function initGraph(){ + let line; + + line = await readline(); + [N, M] = line.split(' ').map(i => parseInt(i)) + graph = new Array(N + 1).fill(0).map(() => new Array(N + 1).fill(0)) + + while(M--){ + line = await readline() + const strArr = line ? line.split(' ').map(i => parseInt(i)) : undefined + strArr ? graph[strArr[0]][strArr[1]] = 1 : null + } +}; + +// 深度搜索 +function dfs(graph, x, n){ + // 当前遍历节点为x, 到达节点为n + if(x == n){ + result.push([...path]) + return + } + for(let i = 1 ; i <= n ; i++){ + if(graph[x][i] == 1){ + path.push(i) + dfs(graph, i, n ) + path.pop(i) + } + } +}; + +(async function(){ + // 创建邻接矩阵,初始化邻接矩阵 + await initGraph(); + + // 从节点1开始深度搜索 + path.push(1); + + // 深度搜索 + dfs(graph, 1, N ); + + // 输出 + if(result.length > 0){ + result.forEach(i => { + console.log(i.join(' ')) + }) + }else{ + console.log(-1) + } + +})(); + +``` + + + +#### 邻接表写法 + +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph; +let N, M; + +// 收集符合条件的路径 +let result = []; +// 1节点到终点的路径 +let path = []; + +// 创建邻接表,初始化邻接表 +async function initGraph() { + let line; + line = await readline(); + [N, M] = line.split(' ').map(i => parseInt(i)) + graph = new Array(N + 1).fill(0).map(() => new Array()) + + while (line = await readline()) { + const strArr = line.split(' ').map(i => parseInt(i)) + strArr ? graph[strArr[0]].push(strArr[1]) : null + } +}; + +// 深度搜索 +async function dfs(graph, x, n) { + // 当前遍历节点为x, 到达节点为n + if (x == n) { + result.push([...path]) + return + } + + graph[x].forEach(i => { + path.push(i) + dfs(graph, i, n) + path.pop(i) + }) +}; + +(async function () { + // 创建邻接表,初始化邻接表 + await initGraph(); + + // 从节点1开始深度搜索 + path.push(1); + + // 深度搜索 + dfs(graph, 1, N); + + // 输出 + if (result.length > 0) { + result.forEach(i => { + console.log(i.join(' ')) + }) + } else { + console.log(-1) + } +})(); +``` + ### TypeScript ### PhP @@ -628,4 +767,3 @@ if __name__ == "__main__": - diff --git a/problems/kamacoder/0099.岛屿的数量广搜.md b/problems/kamacoder/0099.岛屿的数量广搜.md index fc7f38e9..b9bdb796 100644 --- a/problems/kamacoder/0099.岛屿的数量广搜.md +++ b/problems/kamacoder/0099.岛屿的数量广搜.md @@ -198,6 +198,87 @@ int main() { ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph +let N, M +let visited +let result = 0 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + + +/** + * @description: 从(x, y)开始广度优先遍历 + * @param {*} graph 地图 + * @param {*} visited 访问过的节点 + * @param {*} x 开始搜索节点的下标 + * @param {*} y 开始搜索节点的下标 + * @return {*} + */ +const bfs = (graph, visited, x, y) => { + let queue = [] + queue.push([x, y]) + visited[x][y] = true //只要加入队列就立刻标记为访问过 + + while (queue.length) { + let [x, y] = queue.shift() + for (let i = 0; i < 4; i++) { + let nextx = x + dir[i][0] + let nexty = y + dir[i][1] + if(nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if(!visited[nextx][nexty] && graph[nextx][nexty] === 1){ + queue.push([nextx, nexty]) + visited[nextx][nexty] = true + } + } + } + +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计岛屿数 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { + // 遇到没访问过的陆地,+1 + result++ + + // 广度优先遍历,将相邻陆地标记为已访问 + bfs(graph, visited, i, j) + } + } + } + console.log(result); +})() +``` + + + ### TypeScript ### PhP diff --git a/problems/kamacoder/0099.岛屿的数量深搜.md b/problems/kamacoder/0099.岛屿的数量深搜.md index 94ce8d5b..b257ca9a 100644 --- a/problems/kamacoder/0099.岛屿的数量深搜.md +++ b/problems/kamacoder/0099.岛屿的数量深搜.md @@ -285,6 +285,81 @@ if __name__ == '__main__': ### Javascript +```javascript +const r1 = require('readline').createInterface({ input: process.stdin }); +// 创建readline接口 +let iter = r1[Symbol.asyncIterator](); +// 创建异步迭代器 +const readline = async () => (await iter.next()).value; + +let graph +let N, M +let visited +let result = 0 +const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] + +// 读取输入,初始化地图 +const initGraph = async () => { + let line = await readline(); + [N, M] = line.split(' ').map(Number); + graph = new Array(N).fill(0).map(() => new Array(M).fill(0)) + visited = new Array(N).fill(false).map(() => new Array(M).fill(false)) + + for (let i = 0; i < N; i++) { + line = await readline() + line = line.split(' ').map(Number) + for (let j = 0; j < M; j++) { + graph[i][j] = line[j] + } + } +} + +/** + * @description: 从节点x,y开始深度优先遍历 + * @param {*} graph 是地图,也就是一个二维数组 + * @param {*} visited 标记访问过的节点,不要重复访问 + * @param {*} x 表示开始搜索节点的下标 + * @param {*} y 表示开始搜索节点的下标 + * @return {*} + */ +const dfs = (graph, visited, x, y) => { + for (let i = 0; i < 4; i++) { + const nextx = x + dir[i][0] + const nexty = y + dir[i][1] + if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue + if (!visited[nextx][nexty] && graph[nextx][nexty] === 1) { + visited[nextx][nexty] = true + dfs(graph, visited, nextx, nexty) + } + } +} + +(async function () { + + // 读取输入,初始化地图 + await initGraph() + + // 统计岛屿数 + for (let i = 0; i < N; i++) { + for (let j = 0; j < M; j++) { + if (!visited[i][j] && graph[i][j] === 1) { + // 标记已访问 + visited[i][j] = true + + // 遇到没访问过的陆地,+1 + result++ + + // 深度优先遍历,将相邻陆地标记为已访问 + dfs(graph, visited, i, j) + } + } + } + console.log(result); +})() +``` + + + ### TypeScript ### PhP diff --git a/problems/kamacoder/0106.岛屿的周长.md b/problems/kamacoder/0106.岛屿的周长.md index 34063039..48400a95 100644 --- a/problems/kamacoder/0106.岛屿的周长.md +++ b/problems/kamacoder/0106.岛屿的周长.md @@ -66,7 +66,7 @@ ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20240524120105.png) -该录友的下边空格出界了,则说明找到一条边。 +该陆地的下边空格出界了,则说明找到一条边。 C++代码如下:(详细注释)