結果

問題 No.157 2つの空洞
ユーザー バカらっくバカらっく
提出日時 2019-11-09 03:03:04
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 300 ms / 2,000 ms
コード長 2,577 bytes
コンパイル時間 16,520 ms
コンパイル使用メモリ 426,208 KB
実行使用メモリ 54,264 KB
最終ジャッジ日時 2023-10-13 06:39:33
合計ジャッジ時間 19,699 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 260 ms
53,032 KB
testcase_01 AC 278 ms
53,040 KB
testcase_02 AC 255 ms
53,112 KB
testcase_03 AC 255 ms
53,108 KB
testcase_04 AC 248 ms
53,080 KB
testcase_05 AC 254 ms
53,064 KB
testcase_06 AC 249 ms
53,116 KB
testcase_07 AC 248 ms
53,160 KB
testcase_08 AC 250 ms
53,316 KB
testcase_09 AC 251 ms
53,100 KB
testcase_10 AC 255 ms
53,116 KB
testcase_11 AC 255 ms
53,028 KB
testcase_12 AC 267 ms
53,384 KB
testcase_13 AC 258 ms
53,276 KB
testcase_14 AC 258 ms
53,072 KB
testcase_15 AC 259 ms
53,128 KB
testcase_16 AC 256 ms
53,156 KB
testcase_17 AC 300 ms
54,264 KB
testcase_18 AC 262 ms
53,324 KB
testcase_19 AC 256 ms
53,068 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'arr' is never used
fun main(arr:Array<String>) {
         ^

ソースコード

diff #

import java.util.*

fun main(arr:Array<String>) {
    val (w,h) = readLine()!!.split(" ").map { it.toInt() }
    val map = (1..h).map { readLine()!!.toCharArray() }
    val groupMap = (1..h).map { (1..w).map { 0 }.toMutableList() }.toMutableList()
    var mapIndex = 1
    for(r in map.indices) {
        for(c in map[r].indices) {
            if(map[r][c] == '#') {
                continue
            }
            if(groupMap[r][c] > 0) {
                continue
            }
            val queue = LinkedList<Pos>()
            queue.add(Pos(r,c))
            groupMap[r][c] = mapIndex
            while (queue.isNotEmpty()) {
                val current = queue.pop()
                for(newR in (current.row-1..current.row+1)) {
                    if(newR < 0) {
                        continue
                    }
                    if(newR >= h) {
                        continue
                    }
                    for(newC in (current.col-1..current.col+1)) {
                        if(newC < 0) {
                            continue
                        }
                        if(newC >= w) {
                            continue
                        }
                        if(newR == current.row && newC == current.col) {
                            continue
                        }
                        if(newR != current.row && newC != current.col) {
                            continue
                        }
                        if(groupMap[newR][newC] > 0) {
                            continue
                        }
                        if(map[newR][newC] == '#') {
                            continue
                        }
                        groupMap[newR][newC] = mapIndex
                        queue.push(Pos(newR,newC))
                    }
                }
            }
            mapIndex ++
        }
    }
    var ans = w*h
    for(r1 in map.indices) {
        for(c1 in map[r1].indices) {
            if(map[r1][c1] == '#') {
                continue
            }
            for(r2 in map.indices) {
                for(c2 in map[r2].indices) {
                    if(map[r2][c2] == '#') {
                        continue
                    }
                    if(groupMap[r1][c1] == groupMap[r2][c2]) {
                        continue
                    }
                    val dist = Math.abs(r1-r2) + Math.abs(c1-c2) - 1
                    ans = Math.min(ans, dist)
                }
            }
        }
    }
    println(ans)
}

class Pos(val row:Int, val col:Int)
0