Submission #71698377


Source Code Expand

fun main() {
    val cin = System.`in`.bufferedReader()
    val cout = System.out.bufferedWriter()

    val (h, w) = cin.readLine().split(" ").map { it.toInt() }
    val g = Array(h) { cin.readLine().toCharArray() }
    val p = Array(26) { mutableListOf<Pair<Int, Int>>() }
    for (r in 0 until h) for (c in 0 until w) {
        val ch = g[r][c] - 'a'
        if (ch in 0..<26) p[ch].add(r to c)
    }
//    cout.write("p:${p.joinToString(" ")}\n")
    val added = Array(h) { BooleanArray(w) }
    added[0][0] = true
    var ans = 0
    val q = ArrayDeque<Pair<Int, Int>>()
    q.add(0 to 0)
    var found = false
    val d = intArrayOf(-1, 0, 1, 0, -1)
    while (!q.isEmpty()) {
        var k = q.size
        while (k-- > 0) {
            val (r, c) = q.removeFirst()
//            cout.write("visiting $r $c ${g[r][c]}\n")
            if (r == h-1 && c == w-1) {
                found = true
                break
            }
            for (j in 0 until 4) {
                val nr = r + d[j]
                val nc = c + d[j + 1]
                if (nr !in 0..<h || nc !in 0..<w) continue
                if (g[nr][nc] == '#' || added[nr][nc]) continue
                q.addLast(nr to nc)
//                cout.write("added $nr $nc ${g[nr][nc]}\n")
                added[nr][nc] = true
            }
            val ch = g[r][c] - 'a'
//            cout.write("$r $c ch:$ch\n")
            if (ch in 0..<26) {
//                cout.write("warping from $r $c $ch\n")
                for ((wr, wc) in p[ch]) {
                    if (added[wr][wc]) continue
                    q.addLast(wr to wc)
                    added[wr][wc] = true
//                    cout.write("added by warping $wr $wc ${g[wr][wc]}\n")
                }
                p[ch].clear()
            }
        }
        if (found) break
        ans++
    }
    if (!found) ans = -1
    cout.write("${ans}\n")
    cout.flush()
}

Submission Info

Submission Time
Task D - Teleport Maze
User jagbarrameda
Language Kotlin (Kotlin/JVM 2.2.10)
Score 400
Code Size 1970 Byte
Status AC
Exec Time 520 ms
Memory 194080 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 4
AC × 51
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 02_handmade_00.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 91 ms 46536 KiB
00_sample_01.txt AC 88 ms 46528 KiB
00_sample_02.txt AC 86 ms 46368 KiB
00_sample_03.txt AC 85 ms 46468 KiB
01_random_00.txt AC 84 ms 46420 KiB
01_random_01.txt AC 222 ms 70528 KiB
01_random_02.txt AC 264 ms 68652 KiB
01_random_03.txt AC 497 ms 179372 KiB
01_random_04.txt AC 263 ms 70936 KiB
01_random_05.txt AC 257 ms 85552 KiB
01_random_06.txt AC 293 ms 83568 KiB
01_random_07.txt AC 309 ms 74776 KiB
01_random_08.txt AC 88 ms 46624 KiB
01_random_09.txt AC 222 ms 65456 KiB
01_random_10.txt AC 259 ms 69444 KiB
01_random_11.txt AC 401 ms 162060 KiB
01_random_12.txt AC 304 ms 76896 KiB
01_random_13.txt AC 260 ms 68768 KiB
01_random_14.txt AC 287 ms 78056 KiB
01_random_15.txt AC 273 ms 71760 KiB
01_random_16.txt AC 90 ms 46540 KiB
01_random_17.txt AC 170 ms 56296 KiB
01_random_18.txt AC 157 ms 54000 KiB
01_random_19.txt AC 426 ms 130540 KiB
01_random_20.txt AC 287 ms 102824 KiB
01_random_21.txt AC 302 ms 114388 KiB
01_random_22.txt AC 159 ms 58664 KiB
01_random_23.txt AC 254 ms 87888 KiB
01_random_24.txt AC 86 ms 46340 KiB
01_random_25.txt AC 122 ms 51376 KiB
01_random_26.txt AC 149 ms 53684 KiB
01_random_27.txt AC 332 ms 113732 KiB
01_random_28.txt AC 321 ms 104116 KiB
01_random_29.txt AC 300 ms 87496 KiB
01_random_30.txt AC 293 ms 101052 KiB
01_random_31.txt AC 317 ms 102420 KiB
01_random_32.txt AC 88 ms 46504 KiB
01_random_33.txt AC 177 ms 60164 KiB
01_random_34.txt AC 158 ms 54456 KiB
01_random_35.txt AC 272 ms 86504 KiB
01_random_36.txt AC 191 ms 58192 KiB
01_random_37.txt AC 212 ms 62044 KiB
01_random_38.txt AC 262 ms 75884 KiB
01_random_39.txt AC 202 ms 73652 KiB
02_handmade_00.txt AC 266 ms 68908 KiB
02_handmade_01.txt AC 458 ms 194080 KiB
02_handmade_02.txt AC 149 ms 54004 KiB
02_handmade_03.txt AC 148 ms 54996 KiB
02_handmade_04.txt AC 241 ms 68304 KiB
02_handmade_05.txt AC 383 ms 70032 KiB
02_handmade_06.txt AC 520 ms 131868 KiB