結果

問題 No.3 ビットすごろく
コンテスト
ユーザー Pump0129
提出日時 2018-03-30 07:15:39
言語 Kotlin
(2.3.10)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
AC  
実行時間 228 ms / 5,000 ms
コード長 1,105 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,880 ms
コンパイル使用メモリ 444,536 KB
実行使用メモリ 56,692 KB
最終ジャッジ日時 2026-03-22 05:58:36
合計ジャッジ時間 16,747 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package net.ipipip0129.kotlin.yukicoder

// 幅優先探索
fun main(args: Array<String>) {
    val len = readLine()!!.toInt()
    val queue = mutableListOf(1)
    // ループ回避用
    val isPass = Array(len, {false})
    // カウント用
    val countList = mutableListOf(1)
    isPass[0] = true


    while (queue.size != 0) {
        val np = queue[0]
        queue.removeAt(0)
        val count = countList[0]
        countList.removeAt(0)

        if (np == len) {
            println(count)
            return
        }

        val vp = Integer.toBinaryString(np).filter { s -> s == '1' }.length

        if (np + vp <= len) {
            if (!isPass[np + vp - 1]) {
                queue.add(np + vp)
                countList.add(count + 1)
                isPass[np + vp - 1] = true
            }
        }

        if (0 < np - vp) {
            if (!isPass[np - vp - 1]) {
                queue.add(np - vp)
                countList.add(count + 1)
                isPass[np - vp - 1] = true
            }
        }
    }

    println("-1")
}
0