Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 08a437e

Browse files
添加 卡码网0099.岛屿的数量广搜 0099.岛屿的数量深搜 JS版
1 parent 4a831be commit 08a437e

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

‎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
@@ -191,6 +191,81 @@ int main() {
191191

192192
### Javascript
193193

194+
```javascript
195+
const r1 = require('readline').createInterface({ input: process.stdin });
196+
// 创建readline接口
197+
let iter = r1[Symbol.asyncIterator]();
198+
// 创建异步迭代器
199+
const readline = async () => (await iter.next()).value;
200+
201+
let graph
202+
let N, M
203+
let visited
204+
let result = 0
205+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]
206+
207+
// 读取输入,初始化地图
208+
const initGraph = async () => {
209+
let line = await readline();
210+
[N, M] = line.split(' ').map(Number);
211+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
212+
visited = new Array(N).fill(false).map(() => new Array(M).fill(false))
213+
214+
for (let i = 0; i < N; i++) {
215+
line = await readline()
216+
line = line.split(' ').map(Number)
217+
for (let j = 0; j < M; j++) {
218+
graph[i][j] = line[j]
219+
}
220+
}
221+
}
222+
223+
/**
224+
* @description: 从节点x,y开始深度优先遍历
225+
* @param {*} graph 是地图,也就是一个二维数组
226+
* @param {*} visited 标记访问过的节点,不要重复访问
227+
* @param {*} x 表示开始搜索节点的下标
228+
* @param {*} y 表示开始搜索节点的下标
229+
* @return {*}
230+
*/
231+
const dfs = (graph, visited, x, y) => {
232+
for (let i = 0; i < 4; i++) {
233+
const nextx = x + dir[i][0]
234+
const nexty = y + dir[i][1]
235+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
236+
if (!visited[nextx][nexty] && graph[nextx][nexty] === 1) {
237+
visited[nextx][nexty] = true
238+
dfs(graph, visited, nextx, nexty)
239+
}
240+
}
241+
}
242+
243+
(async function () {
244+
245+
// 读取输入,初始化地图
246+
await initGraph()
247+
248+
// 统计岛屿数
249+
for (let i = 0; i < N; i++) {
250+
for (let j = 0; j < M; j++) {
251+
if (!visited[i][j] && graph[i][j] === 1) {
252+
// 标记已访问
253+
visited[i][j] = true
254+
255+
// 遇到没访问过的陆地,+1
256+
result++
257+
258+
// 深度优先遍历,将相邻陆地标记为已访问
259+
dfs(graph, visited, i, j)
260+
}
261+
}
262+
}
263+
console.log(result);
264+
})()
265+
```
266+
267+
268+
194269
### TypeScript
195270

196271
### PhP

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /