mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -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__":
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -66,7 +66,7 @@
|
||||
|
||||

|
||||
|
||||
该录友的下边空格出界了,则说明找到一条边。
|
||||
该陆地的下边空格出界了,则说明找到一条边。
|
||||
|
||||
|
||||
C++代码如下:(详细注释)
|
||||
|
Reference in New Issue
Block a user