結果

問題 No.3 ビットすごろく
ユーザー komkomhkomkomh
提出日時 2019-04-07 10:13:52
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 13,308 ms
コンパイル使用メモリ 421,312 KB
実行使用メモリ 61,476 KB
最終ジャッジ日時 2023-08-13 01:39:50
合計ジャッジ時間 32,749 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
52,796 KB
testcase_01 AC 280 ms
52,904 KB
testcase_02 AC 279 ms
52,708 KB
testcase_03 AC 284 ms
52,840 KB
testcase_04 WA -
testcase_05 AC 292 ms
53,028 KB
testcase_06 AC 286 ms
52,876 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 289 ms
52,884 KB
testcase_13 AC 293 ms
52,848 KB
testcase_14 AC 292 ms
52,900 KB
testcase_15 AC 300 ms
52,960 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 277 ms
52,816 KB
testcase_22 AC 294 ms
53,064 KB
testcase_23 AC 305 ms
53,136 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 285 ms
52,680 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder

import kotlin.math.absoluteValue

fun main() {
    val n: Int = readLine()!!.toInt()

    val sugoroku: List<Int> = (1..n).map { getBit(it) }
    var cache: MutableSet<Int> = mutableSetOf()
    fun canReachFromZero(target: Int, deep: Int): Int {
        return when {
            target == 0 -> deep
            cache.contains(target) -> -1
            else -> {
                cache.add(target)
                val fromList: List<Int> = (0 until sugoroku.size).filter { (target - it).absoluteValue == sugoroku[it] }
                fromList.map { canReachFromZero(it, deep + 1) }.find { it != -1 } ?: run { -1 }
            }
        }
    }
    println(canReachFromZero(n - 1, 1))
}

fun getBit(number: Int): Int =
    when (number) {
        0 -> 0
        1 -> 1
        else -> getBit(number / 2) + number % 2
    }
0