@@ -242,6 +242,173 @@ print(count)
242
242
243
243
### Javascript
244
244
245
+ #### 深搜版
246
+
247
+ ``` javascript
248
+ const r1 = require (' readline' ).createInterface ({ input: process .stdin });
249
+ // 创建readline接口
250
+ let iter = r1[Symbol .asyncIterator ]();
251
+ // 创建异步迭代器
252
+ const readline = async () => (await iter .next ()).value ;
253
+
254
+ let graph // 地图
255
+ let N , M // 地图大小
256
+ let count = 0 // 孤岛的总面积
257
+ const dir = [[0 , 1 ], [1 , 0 ], [0 , - 1 ], [- 1 , 0 ]] // 方向
258
+
259
+
260
+ // 读取输入,初始化地图
261
+ const initGraph = async () => {
262
+ let line = await readline ();
263
+ [N , M ] = line .split (' ' ).map (Number );
264
+ graph = new Array (N ).fill (0 ).map (() => new Array (M ).fill (0 ))
265
+
266
+ for (let i = 0 ; i < N ; i++ ) {
267
+ line = await readline ()
268
+ line = line .split (' ' ).map (Number )
269
+ for (let j = 0 ; j < M ; j++ ) {
270
+ graph[i][j] = line[j]
271
+ }
272
+ }
273
+ }
274
+
275
+
276
+ /**
277
+ * @description : 从(x,y)开始深度优先遍历地图
278
+ * @param {*} graph 地图
279
+ * @param {*} x 开始搜索节点的下标
280
+ * @param {*} y 开始搜索节点的下标
281
+ * @return {*}
282
+ */
283
+ const dfs = (graph , x , y ) => {
284
+ if (graph[x][y] === 0 ) return
285
+ graph[x][y] = 0 // 标记为海洋
286
+ for (let i = 0 ; i < 4 ; i++ ) {
287
+ let nextx = x + dir[i][0 ]
288
+ let nexty = y + dir[i][1 ]
289
+ if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M ) continue
290
+ dfs (graph, nextx, nexty)
291
+ }
292
+ }
293
+
294
+ (async function () {
295
+
296
+ // 读取输入,初始化地图
297
+ await initGraph ()
298
+
299
+ // 遍历地图左右两边
300
+ for (let i = 0 ; i < N ; i++ ) {
301
+ if (graph[i][0 ] === 1 ) dfs (graph, i, 0 )
302
+ if (graph[i][M - 1 ] === 1 ) dfs (graph, i, M - 1 )
303
+ }
304
+
305
+ // 遍历地图上下两边
306
+ for (let j = 0 ; j < M ; j++ ) {
307
+ if (graph[0 ][j] === 1 ) dfs (graph, 0 , j)
308
+ if (graph[N - 1 ][j] === 1 ) dfs (graph, N - 1 , j)
309
+ }
310
+
311
+ count = 0
312
+ // 统计孤岛的总面积
313
+ for (let i = 0 ; i < N ; i++ ) {
314
+ for (let j = 0 ; j < M ; j++ ) {
315
+ if (graph[i][j] === 1 ) count++
316
+ }
317
+ }
318
+ console .log (count);
319
+ })()
320
+ ```
321
+
322
+
323
+
324
+ #### 广搜版
325
+
326
+ ``` javascript
327
+ const r1 = require (' readline' ).createInterface ({ input: process .stdin });
328
+ // 创建readline接口
329
+ let iter = r1[Symbol .asyncIterator ]();
330
+ // 创建异步迭代器
331
+ const readline = async () => (await iter .next ()).value ;
332
+
333
+ let graph // 地图
334
+ let N , M // 地图大小
335
+ let count = 0 // 孤岛的总面积
336
+ const dir = [[0 , 1 ], [1 , 0 ], [0 , - 1 ], [- 1 , 0 ]] // 方向
337
+
338
+
339
+ // 读取输入,初始化地图
340
+ const initGraph = async () => {
341
+ let line = await readline ();
342
+ [N , M ] = line .split (' ' ).map (Number );
343
+ graph = new Array (N ).fill (0 ).map (() => new Array (M ).fill (0 ))
344
+
345
+ for (let i = 0 ; i < N ; i++ ) {
346
+ line = await readline ()
347
+ line = line .split (' ' ).map (Number )
348
+ for (let j = 0 ; j < M ; j++ ) {
349
+ graph[i][j] = line[j]
350
+ }
351
+ }
352
+ }
353
+
354
+
355
+ /**
356
+ * @description : 从(x,y)开始广度优先遍历地图
357
+ * @param {*} graph 地图
358
+ * @param {*} x 开始搜索节点的下标
359
+ * @param {*} y 开始搜索节点的下标
360
+ * @return {*}
361
+ */
362
+ const bfs = (graph , x , y ) => {
363
+ let queue = []
364
+ queue .push ([x, y])
365
+ graph[x][y] = 0 // 只要加入队列,立刻标记
366
+
367
+ while (queue .length ) {
368
+ let [xx, yy] = queue .shift ()
369
+ for (let i = 0 ; i < 4 ; i++ ) {
370
+ let nextx = xx + dir[i][0 ]
371
+ let nexty = yy + dir[i][1 ]
372
+ if (nextx < 0 || nextx >= N || nexty < 0 || nexty >= M ) continue
373
+ if (graph[nextx][nexty] === 1 ) {
374
+ queue .push ([nextx, nexty])
375
+ graph[nextx][nexty] = 0 // 只要加入队列,立刻标记
376
+ }
377
+ }
378
+ }
379
+
380
+ }
381
+
382
+ (async function () {
383
+
384
+ // 读取输入,初始化地图
385
+ await initGraph ()
386
+
387
+ // 遍历地图左右两边
388
+ for (let i = 0 ; i < N ; i++ ) {
389
+ if (graph[i][0 ] === 1 ) bfs (graph, i, 0 )
390
+ if (graph[i][M - 1 ] === 1 ) bfs (graph, i, M - 1 )
391
+ }
392
+
393
+ // 遍历地图上下两边
394
+ for (let j = 0 ; j < M ; j++ ) {
395
+ if (graph[0 ][j] === 1 ) bfs (graph, 0 , j)
396
+ if (graph[N - 1 ][j] === 1 ) bfs (graph, N - 1 , j)
397
+ }
398
+
399
+ count = 0
400
+ // 统计孤岛的总面积
401
+ for (let i = 0 ; i < N ; i++ ) {
402
+ for (let j = 0 ; j < M ; j++ ) {
403
+ if (graph[i][j] === 1 ) count++
404
+ }
405
+ }
406
+ console .log (count);
407
+ })()
408
+ ```
409
+
410
+
411
+
245
412
### TypeScript
246
413
247
414
### PhP
0 commit comments