結果

問題 No.3 ビットすごろく
ユーザー komkomhkomkomh
提出日時 2019-04-14 22:10:18
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 377 ms / 5,000 ms
コード長 872 bytes
コンパイル時間 13,835 ms
コンパイル使用メモリ 414,028 KB
実行使用メモリ 57,980 KB
最終ジャッジ日時 2023-09-14 01:17:16
合計ジャッジ時間 26,647 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 304 ms
57,136 KB
testcase_01 AC 307 ms
56,796 KB
testcase_02 AC 308 ms
56,768 KB
testcase_03 AC 325 ms
57,012 KB
testcase_04 AC 326 ms
57,032 KB
testcase_05 AC 348 ms
57,328 KB
testcase_06 AC 329 ms
57,100 KB
testcase_07 AC 319 ms
57,156 KB
testcase_08 AC 349 ms
57,268 KB
testcase_09 AC 341 ms
57,424 KB
testcase_10 AC 351 ms
57,204 KB
testcase_11 AC 340 ms
57,388 KB
testcase_12 AC 360 ms
57,268 KB
testcase_13 AC 334 ms
57,028 KB
testcase_14 AC 354 ms
57,448 KB
testcase_15 AC 373 ms
57,248 KB
testcase_16 AC 365 ms
57,300 KB
testcase_17 AC 351 ms
57,512 KB
testcase_18 AC 322 ms
56,956 KB
testcase_19 AC 351 ms
57,512 KB
testcase_20 AC 319 ms
57,120 KB
testcase_21 AC 315 ms
56,708 KB
testcase_22 AC 358 ms
57,980 KB
testcase_23 AC 364 ms
57,280 KB
testcase_24 AC 377 ms
57,696 KB
testcase_25 AC 360 ms
57,276 KB
testcase_26 AC 313 ms
56,596 KB
testcase_27 AC 329 ms
57,024 KB
testcase_28 AC 356 ms
57,444 KB
testcase_29 AC 363 ms
57,292 KB
testcase_30 AC 310 ms
56,692 KB
testcase_31 AC 321 ms
56,712 KB
testcase_32 AC 376 ms
57,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fun main() {
    val n: Int = readLine()!!.toInt()
    val sugoroku: List<Int> = (1..n).map { getBit2(it) }
    val queue: MutableList<Pair<Int, Int>> = mutableListOf(Pair(0, 0))
    val cache: MutableSet<Int> = mutableSetOf(0)

    while (queue.isNotEmpty()) {
        val pair = queue.removeAt(0)
        if (pair.first == n - 1) {
            println(pair.second + 1)
            return
        }

        val plusIndex = pair.first + sugoroku[pair.first]
        if (plusIndex < n && cache.add(plusIndex)) {
            queue.add(Pair(plusIndex, pair.second + 1))
        }

        val minusIndex = pair.first - sugoroku[pair.first]
        if (minusIndex > 0 && cache.add(minusIndex)) {
            queue.add(Pair(minusIndex, pair.second + 1))
        }
    }
    println(-1)
}

fun getBit2(number: Int): Int = Integer.toString(number, 2).count { num -> num == '1' }
0