結果

問題 No.3 ビットすごろく
ユーザー komkomh
提出日時 2019-04-14 22:10:18
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 389 ms / 5,000 ms
コード長 872 bytes
コンパイル時間 12,778 ms
コンパイル使用メモリ 442,052 KB
実行使用メモリ 63,032 KB
最終ジャッジ日時 2024-07-01 09:19:04
合計ジャッジ時間 26,546 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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