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 2cc0080

Browse files
Merge pull request youngyangyang04#2677 from nineninee/km0104
添加 卡码网0104.建造最大岛屿 JS版
2 parents 7ddc751 + d90d5fb commit 2cc0080

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
@@ -532,6 +532,123 @@ if __name__ == "__main__":
532532

533533
### Javascript
534534

535+
```javascript
536+
const r1 = require('readline').createInterface({ input: process.stdin });
537+
// 创建readline接口
538+
let iter = r1[Symbol.asyncIterator]();
539+
// 创建异步迭代器
540+
const readline = async () => (await iter.next()).value;
541+
542+
let graph // 地图
543+
let N, M // 地图大小
544+
let visited // 访问过的节点, 标记岛屿
545+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向
546+
547+
let count = 0 // 统计岛屿面积
548+
let areaMap = new Map() // 存储岛屿面积
549+
550+
551+
// 读取输入,初始化地图
552+
const initGraph = async () => {
553+
let line = await readline();
554+
[N, M] = line.split(' ').map(Number);
555+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
556+
visited = new Array(N).fill(0).map(() => new Array(M).fill(0))
557+
558+
for (let i = 0; i < N; i++) {
559+
line = await readline()
560+
line = line.split(' ').map(Number)
561+
for (let j = 0; j < M; j++) {
562+
graph[i][j] = line[j]
563+
}
564+
}
565+
}
566+
567+
/**
568+
* @description: 从(x,y)开始深度优先遍历地图
569+
* @param {*} graph 地图
570+
* @param {*} visited 可访问节点
571+
* @param {*} x 开始搜索节点的下标
572+
* @param {*} y 开始搜索节点的下标
573+
* @param {*} mark 当前岛屿的标记
574+
* @return {*}
575+
*/
576+
const dfs = (graph, visited, x, y, mark) => {
577+
if (visited[x][y] != 0) return
578+
visited[x][y] = mark
579+
count++
580+
581+
for (let i = 0; i < 4; i++) {
582+
let nextx = x + dir[i][0]
583+
let nexty = y + dir[i][1]
584+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过
585+
586+
// 已访问过, 或者是海洋, 跳过
587+
if (visited[nextx][nexty] != 0 || graph[nextx][nexty] == 0) continue
588+
589+
dfs(graph, visited, nextx, nexty, mark)
590+
}
591+
}
592+
593+
(async function () {
594+
595+
// 读取输入,初始化地图
596+
await initGraph()
597+
598+
let isAllLand = true //标记整个地图都是陆地
599+
600+
let mark = 2 // 标记岛屿
601+
for (let i = 0; i < N; i++) {
602+
for (let j = 0; j < M; j++) {
603+
if (graph[i][j] == 0 && isAllLand) isAllLand = false
604+
if (graph[i][j] === 1 && visited[i][j] === 0) {
605+
count = 0
606+
dfs(graph, visited, i, j, mark)
607+
areaMap.set(mark, count)
608+
mark++
609+
}
610+
}
611+
}
612+
613+
// 如果全是陆地, 直接返回面积
614+
if (isAllLand) {
615+
console.log(N * M);
616+
return
617+
}
618+
619+
let result = 0 // 记录最后结果
620+
let visitedIsland = new Map() //标记访问过的岛屿, 因为海洋四周可能是同一个岛屿, 需要标记避免重复统计面积
621+
for (let i = 0; i < N; i++) {
622+
for (let j = 0; j < M; j++) {
623+
if (visited[i][j] === 0) {
624+
count = 1 // 记录连接之后的岛屿数量
625+
visitedIsland.clear() // 每次使用时,清空
626+
627+
// 计算海洋周围岛屿面积
628+
for (let m = 0; m < 4; m++) {
629+
const nextx = i + dir[m][0]
630+
const nexty = j + dir[m][1]
631+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过
632+
633+
const island = visited[nextx][nexty]
634+
if (island == 0 || visitedIsland.get(island)) continue// 四周是海洋或者访问过的陆地 跳过
635+
636+
// 标记为访问, 计算面积
637+
visitedIsland.set(island, true)
638+
count += areaMap.get(visited[nextx][nexty])
639+
}
640+
641+
result = Math.max(result, count)
642+
}
643+
}
644+
}
645+
646+
console.log(result);
647+
})()
648+
```
649+
650+
651+
535652
### TypeScript
536653

537654
### PhP

0 commit comments

Comments
(0)

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