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 d90d5fb

Browse files
添加 卡码网0104.建造最大岛屿 JS版
1 parent 29bfd78 commit d90d5fb

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

‎problems/kamacoder/0104.建造最大岛屿.md‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,123 @@ public class Main {
372372

373373
### Javascript
374374

375+
```javascript
376+
const r1 = require('readline').createInterface({ input: process.stdin });
377+
// 创建readline接口
378+
let iter = r1[Symbol.asyncIterator]();
379+
// 创建异步迭代器
380+
const readline = async () => (await iter.next()).value;
381+
382+
let graph // 地图
383+
let N, M // 地图大小
384+
let visited // 访问过的节点, 标记岛屿
385+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向
386+
387+
let count = 0 // 统计岛屿面积
388+
let areaMap = new Map() // 存储岛屿面积
389+
390+
391+
// 读取输入,初始化地图
392+
const initGraph = async () => {
393+
let line = await readline();
394+
[N, M] = line.split(' ').map(Number);
395+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
396+
visited = new Array(N).fill(0).map(() => new Array(M).fill(0))
397+
398+
for (let i = 0; i < N; i++) {
399+
line = await readline()
400+
line = line.split(' ').map(Number)
401+
for (let j = 0; j < M; j++) {
402+
graph[i][j] = line[j]
403+
}
404+
}
405+
}
406+
407+
/**
408+
* @description: 从(x,y)开始深度优先遍历地图
409+
* @param {*} graph 地图
410+
* @param {*} visited 可访问节点
411+
* @param {*} x 开始搜索节点的下标
412+
* @param {*} y 开始搜索节点的下标
413+
* @param {*} mark 当前岛屿的标记
414+
* @return {*}
415+
*/
416+
const dfs = (graph, visited, x, y, mark) => {
417+
if (visited[x][y] != 0) return
418+
visited[x][y] = mark
419+
count++
420+
421+
for (let i = 0; i < 4; i++) {
422+
let nextx = x + dir[i][0]
423+
let nexty = y + dir[i][1]
424+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过
425+
426+
// 已访问过, 或者是海洋, 跳过
427+
if (visited[nextx][nexty] != 0 || graph[nextx][nexty] == 0) continue
428+
429+
dfs(graph, visited, nextx, nexty, mark)
430+
}
431+
}
432+
433+
(async function () {
434+
435+
// 读取输入,初始化地图
436+
await initGraph()
437+
438+
let isAllLand = true //标记整个地图都是陆地
439+
440+
let mark = 2 // 标记岛屿
441+
for (let i = 0; i < N; i++) {
442+
for (let j = 0; j < M; j++) {
443+
if (graph[i][j] == 0 && isAllLand) isAllLand = false
444+
if (graph[i][j] === 1 && visited[i][j] === 0) {
445+
count = 0
446+
dfs(graph, visited, i, j, mark)
447+
areaMap.set(mark, count)
448+
mark++
449+
}
450+
}
451+
}
452+
453+
// 如果全是陆地, 直接返回面积
454+
if (isAllLand) {
455+
console.log(N * M);
456+
return
457+
}
458+
459+
let result = 0 // 记录最后结果
460+
let visitedIsland = new Map() //标记访问过的岛屿, 因为海洋四周可能是同一个岛屿, 需要标记避免重复统计面积
461+
for (let i = 0; i < N; i++) {
462+
for (let j = 0; j < M; j++) {
463+
if (visited[i][j] === 0) {
464+
count = 1 // 记录连接之后的岛屿数量
465+
visitedIsland.clear() // 每次使用时,清空
466+
467+
// 计算海洋周围岛屿面积
468+
for (let m = 0; m < 4; m++) {
469+
const nextx = i + dir[m][0]
470+
const nexty = j + dir[m][1]
471+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过
472+
473+
const island = visited[nextx][nexty]
474+
if (island == 0 || visitedIsland.get(island)) continue// 四周是海洋或者访问过的陆地 跳过
475+
476+
// 标记为访问, 计算面积
477+
visitedIsland.set(island, true)
478+
count += areaMap.get(visited[nextx][nexty])
479+
}
480+
481+
result = Math.max(result, count)
482+
}
483+
}
484+
}
485+
486+
console.log(result);
487+
})()
488+
```
489+
490+
491+
375492
### TypeScript
376493

377494
### PhP

0 commit comments

Comments
(0)

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