結果

問題 No.3 ビットすごろく
ユーザー komkomhkomkomh
提出日時 2019-04-07 10:13:52
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 840 bytes
コンパイル時間 10,053 ms
コンパイル使用メモリ 436,960 KB
実行使用メモリ 88,072 KB
最終ジャッジ日時 2024-04-30 20:13:09
合計ジャッジ時間 28,140 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
56,580 KB
testcase_01 AC 269 ms
56,636 KB
testcase_02 AC 275 ms
56,664 KB
testcase_03 AC 280 ms
56,932 KB
testcase_04 WA -
testcase_05 AC 274 ms
56,944 KB
testcase_06 AC 289 ms
57,188 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 267 ms
56,896 KB
testcase_13 AC 267 ms
56,912 KB
testcase_14 AC 271 ms
57,092 KB
testcase_15 AC 278 ms
56,920 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 261 ms
56,684 KB
testcase_22 AC 269 ms
56,984 KB
testcase_23 AC 282 ms
56,972 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 258 ms
56,676 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