結果

問題 No.3 ビットすごろく
ユーザー Pump0129
提出日時 2018-03-30 07:15:39
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 414 ms / 5,000 ms
コード長 1,105 bytes
コンパイル時間 11,917 ms
コンパイル使用メモリ 427,496 KB
実行使用メモリ 56,172 KB
最終ジャッジ日時 2024-07-01 09:00:02
合計ジャッジ時間 24,976 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:4:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

diff #

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