結果

問題 No.3 ビットすごろく
ユーザー komkomh
提出日時 2019-04-07 10:13:52
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 12,057 ms
コンパイル使用メモリ 438,408 KB
実行使用メモリ 88,104 KB
最終ジャッジ日時 2024-11-20 17:51:22
合計ジャッジ時間 33,355 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 19
権限があれば一括ダウンロードができます

ソースコード

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