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 8a7b531

Browse files
Merge pull request youngyangyang04#2633 from nineninee/km0102
添加 卡码网0102.沉没孤岛 JS版
2 parents eea315e + 3dce79d commit 8a7b531

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

‎problems/kamacoder/0102.沉没孤岛.md‎

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,171 @@ int main() {
146146

147147
### Javascript
148148

149+
#### 深搜版
150+
151+
```javascript
152+
const r1 = require('readline').createInterface({ input: process.stdin });
153+
// 创建readline接口
154+
let iter = r1[Symbol.asyncIterator]();
155+
// 创建异步迭代器
156+
const readline = async () => (await iter.next()).value;
157+
158+
let graph // 地图
159+
let N, M // 地图大小
160+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向
161+
162+
163+
// 读取输入,初始化地图
164+
const initGraph = async () => {
165+
let line = await readline();
166+
[N, M] = line.split(' ').map(Number);
167+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
168+
169+
for (let i = 0; i < N; i++) {
170+
line = await readline()
171+
line = line.split(' ').map(Number)
172+
for (let j = 0; j < M; j++) {
173+
graph[i][j] = line[j]
174+
}
175+
}
176+
}
177+
178+
179+
/**
180+
* @description: 从(x,y)开始深度优先遍历地图
181+
* @param {*} graph 地图
182+
* @param {*} x 开始搜索节点的下标
183+
* @param {*} y 开始搜索节点的下标
184+
* @return {*}
185+
*/
186+
const dfs = (graph, x, y) => {
187+
if (graph[x][y] !== 1) return
188+
graph[x][y] = 2 // 标记为非孤岛陆地
189+
190+
for (let i = 0; i < 4; i++) {
191+
let nextx = x + dir[i][0]
192+
let nexty = y + dir[i][1]
193+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
194+
dfs(graph, nextx, nexty)
195+
}
196+
}
197+
198+
(async function () {
199+
200+
// 读取输入,初始化地图
201+
await initGraph()
202+
203+
// 遍历地图左右两边
204+
for (let i = 0; i < N; i++) {
205+
if (graph[i][0] === 1) dfs(graph, i, 0)
206+
if (graph[i][M - 1] === 1) dfs(graph, i, M - 1)
207+
}
208+
209+
// 遍历地图上下两边
210+
for (let j = 0; j < M; j++) {
211+
if (graph[0][j] === 1) dfs(graph, 0, j)
212+
if (graph[N - 1][j] === 1) dfs(graph, N - 1, j)
213+
}
214+
215+
216+
// 遍历地图,将孤岛沉没,即将陆地1标记为0;将非孤岛陆地2标记为1
217+
for (let i = 0; i < N; i++) {
218+
for (let j = 0; j < M; j++) {
219+
if (graph[i][j] === 1) graph[i][j] = 0
220+
else if (graph[i][j] === 2) graph[i][j] = 1
221+
}
222+
console.log(graph[i].join(' '));
223+
}
224+
})()
225+
```
226+
227+
228+
229+
#### 广搜版
230+
231+
```javascript
232+
const r1 = require('readline').createInterface({ input: process.stdin });
233+
// 创建readline接口
234+
let iter = r1[Symbol.asyncIterator]();
235+
// 创建异步迭代器
236+
const readline = async () => (await iter.next()).value;
237+
238+
let graph // 地图
239+
let N, M // 地图大小
240+
const dir = [[0, 1], [1, 0], [0, -1], [-1, 0]] //方向
241+
242+
243+
// 读取输入,初始化地图
244+
const initGraph = async () => {
245+
let line = await readline();
246+
[N, M] = line.split(' ').map(Number);
247+
graph = new Array(N).fill(0).map(() => new Array(M).fill(0))
248+
249+
for (let i = 0; i < N; i++) {
250+
line = await readline()
251+
line = line.split(' ').map(Number)
252+
for (let j = 0; j < M; j++) {
253+
graph[i][j] = line[j]
254+
}
255+
}
256+
}
257+
258+
259+
/**
260+
* @description: 从(x,y)开始广度优先遍历地图
261+
* @param {*} graph 地图
262+
* @param {*} x 开始搜索节点的下标
263+
* @param {*} y 开始搜索节点的下标
264+
* @return {*}
265+
*/
266+
const bfs = (graph, x, y) => {
267+
let queue = []
268+
queue.push([x, y])
269+
graph[x][y] = 2 // 标记为非孤岛陆地
270+
271+
while (queue.length) {
272+
let [xx, yy] = queue.shift()
273+
274+
for (let i = 0; i < 4; i++) {
275+
let nextx = xx + dir[i][0]
276+
let nexty = yy + dir[i][1]
277+
if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M) continue
278+
if (graph[nextx][nexty] === 1) bfs(graph, nextx, nexty)
279+
}
280+
}
281+
}
282+
283+
(async function () {
284+
285+
// 读取输入,初始化地图
286+
await initGraph()
287+
288+
// 遍历地图左右两边
289+
for (let i = 0; i < N; i++) {
290+
if (graph[i][0] === 1) bfs(graph, i, 0)
291+
if (graph[i][M - 1] === 1) bfs(graph, i, M - 1)
292+
}
293+
294+
// 遍历地图上下两边
295+
for (let j = 0; j < M; j++) {
296+
if (graph[0][j] === 1) bfs(graph, 0, j)
297+
if (graph[N - 1][j] === 1) bfs(graph, N - 1, j)
298+
}
299+
300+
301+
// 遍历地图,将孤岛沉没,即将陆地1标记为0;将非孤岛陆地2标记为1
302+
for (let i = 0; i < N; i++) {
303+
for (let j = 0; j < M; j++) {
304+
if (graph[i][j] === 1) graph[i][j] = 0
305+
else if (graph[i][j] === 2) graph[i][j] = 1
306+
}
307+
console.log(graph[i].join(' '));
308+
}
309+
})()
310+
```
311+
312+
313+
149314
### TypeScript
150315

151316
### PhP

0 commit comments

Comments
(0)

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