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

[pull] master from youngyangyang04:master #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jenningsloy318 merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
00405e5
更新0094.城市间货物运输I.md Java版本解法
learnerInTheFirstStage Aug 7, 2024
2ecb6a5
更新0094.城市间货物运输I-SPFA Java版本解法
learnerInTheFirstStage Aug 7, 2024
d90d5fb
添加 卡码网0104.建造最大岛屿 JS版
nineninee Aug 8, 2024
aff4169
添加 卡码网0107.寻找存在的路径 JS版
nineninee Aug 8, 2024
f3dae1f
Merge branch 'youngyangyang04:master' into master
learnerInTheFirstStage Aug 8, 2024
047c57a
更新0095.城市间货物运输II 基于DPFA版本的Java解法
learnerInTheFirstStage Aug 8, 2024
d2163ac
更新0095.城市间货物运输II 基于SPFA版本的Java解法
learnerInTheFirstStage Aug 8, 2024
ab6cb84
更新0096.城市间货物运输III 基于Bellman_ford一般解法Java版本
learnerInTheFirstStage Aug 9, 2024
7ddc751
Merge pull request #2675 from learnerInTheFirstStage/master
youngyangyang04 Aug 21, 2024
2cc0080
Merge pull request #2677 from nineninee/km0104
youngyangyang04 Aug 21, 2024
488f040
Merge pull request #2678 from nineninee/km0107
youngyangyang04 Aug 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
添加 卡码网0104.建造最大岛屿 JS版
  • Loading branch information
nineninee committed Aug 8, 2024
commit d90d5fbdca5a82420521a960fc04f8fe067aaacf
117 changes: 117 additions & 0 deletions problems/kamacoder/0104.建造最大岛屿.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,123 @@ public class 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 // 访问过的节点, 标记岛屿
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向

let count = 0 // 统计岛屿面积
let areaMap = new Map() // 存储岛屿面积


// 读取输入,初始化地图
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(0).map(() => new Array(M).fill(0))

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 开始搜索节点的下标
* @param {*} mark 当前岛屿的标记
* @return {*}
*/
const dfs = (graph, visited, x, y, mark) => {
if (visited[x][y] != 0) return
visited[x][y] = mark
count++

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] != 0 || graph[nextx][nexty] == 0) continue

dfs(graph, visited, nextx, nexty, mark)
}
}

(async function () {

// 读取输入,初始化地图
await initGraph()

let isAllLand = true //标记整个地图都是陆地

let mark = 2 // 标记岛屿
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (graph[i][j] == 0 && isAllLand) isAllLand = false
if (graph[i][j] === 1 && visited[i][j] === 0) {
count = 0
dfs(graph, visited, i, j, mark)
areaMap.set(mark, count)
mark++
}
}
}

// 如果全是陆地, 直接返回面积
if (isAllLand) {
console.log(N * M);
return
}

let result = 0 // 记录最后结果
let visitedIsland = new Map() //标记访问过的岛屿, 因为海洋四周可能是同一个岛屿, 需要标记避免重复统计面积
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (visited[i][j] === 0) {
count = 1 // 记录连接之后的岛屿数量
visitedIsland.clear() // 每次使用时,清空

// 计算海洋周围岛屿面积
for (let m = 0; m < 4; m++) {
const nextx = i + dir[m][0]
const nexty = j + dir[m][1]
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue //越界, 跳过

const island = visited[nextx][nexty]
if (island == 0 || visitedIsland.get(island)) continue// 四周是海洋或者访问过的陆地 跳过

// 标记为访问, 计算面积
visitedIsland.set(island, true)
count += areaMap.get(visited[nextx][nexty])
}

result = Math.max(result, count)
}
}
}

console.log(result);
})()
```



### TypeScript

### PhP
Expand Down

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