結果

問題 No.13 囲みたい!
ユーザー バカらっくバカらっく
提出日時 2019-08-31 18:06:12
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,032 ms / 5,000 ms
コード長 2,216 bytes
コンパイル時間 14,511 ms
コンパイル使用メモリ 452,060 KB
実行使用メモリ 92,436 KB
最終ジャッジ日時 2024-05-03 20:58:05
合計ジャッジ時間 23,801 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 321 ms
56,944 KB
testcase_01 AC 326 ms
57,184 KB
testcase_02 AC 320 ms
57,080 KB
testcase_03 AC 1,032 ms
91,844 KB
testcase_04 AC 519 ms
71,688 KB
testcase_05 AC 935 ms
92,436 KB
testcase_06 AC 545 ms
85,204 KB
testcase_07 AC 542 ms
82,972 KB
testcase_08 AC 496 ms
65,572 KB
testcase_09 AC 523 ms
65,744 KB
testcase_10 AC 362 ms
57,276 KB
testcase_11 AC 490 ms
63,656 KB
testcase_12 AC 344 ms
57,140 KB
testcase_13 AC 422 ms
61,508 KB
testcase_14 AC 425 ms
61,404 KB
testcase_15 AC 327 ms
56,992 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:3:10: warning: parameter 'args' is never used
fun main(args: Array<String>){
         ^
Main.kt:46:9: warning: variable 'num' is never used
    val num = map[y][x]
        ^

ソースコード

diff #

import java.util.*

fun main(args: Array<String>){
    val (a1, a2) = readLine()!!.split(" ").map { it.toInt() }
    w = a1
    h = a2

    for(i in 1..h) {
        map.add(readLine()!!.trim().split(" ").map { it.toInt() }.toTypedArray())
        flags.add((1..w).map { false }.toTypedArray())
    }

    for(i in 0..h-1) {
        for(j in 0..w-1) {
            val history = mutableMapOf<Int, MutableMap<Int, Boolean>>()
            history[i] = mutableMapOf()
            history[i]!![j] = true
            stk.push(Task(i ,j, -1, -1, history))
            while (stk.isNotEmpty()) {
                val tsk = stk.pop()
                getAns(tsk.y, tsk.x, tsk.prevY, tsk.prevX, tsk.history)
            }
        }
    }
    if(ans) {
        println("possible")
    } else {
        println("impossible")
    }
}
class Task(val y: Int, val x: Int, val prevY: Int, val prevX: Int, val history: MutableMap<Int, MutableMap<Int, Boolean>>)
val stk = Stack<Task>()
var w = 0
var h = 0
var ans = false
val map = mutableListOf<Array<Int>>()
var flags = mutableListOf<Array<Boolean>>()
fun getAns(y:Int, x:Int, prevY:Int, prevX:Int, history:MutableMap<Int, MutableMap<Int, Boolean>>) {
    if(ans) {
        return
    }
    if(flags[y][x]) {
        return
    }
    flags[y][x] = true
    val num = map[y][x]
    for(i in Math.max(0, y-1)..Math.min(h-1, y+1)) {
        for(j in Math.max(0, x-1)..Math.min(w-1, x+1)) {
            if(i!=y && j!=x) {
                continue
            }
            if(i==y&&j==x) {
                continue
            }
            if(i ==prevY && j ==prevX) {
                continue
            }
            if(map[i][j] != map[y][x]) {
                continue
            }
            if(history.containsKey(i) && history[i]!!.containsKey(j)) {
                ans = true
                return
            }
            val subHistory = mutableMapOf<Int, MutableMap<Int, Boolean>>()
            history.forEach { subHistory[it.key] = it.value.toMutableMap() }
            if(!subHistory.containsKey(i)) {
                subHistory[i] = mutableMapOf()
            }
            subHistory[i]!![j] = true
            stk.push(Task(i, j, y, x, subHistory))
        }
    }
}
0