結果

問題 No.3 ビットすごろく
ユーザー tana_twtrtana_twtr
提出日時 2016-10-30 18:03:02
言語 Ruby
(3.3.0)
結果
AC  
実行時間 104 ms / 5,000 ms
コード長 821 bytes
コンパイル時間 43 ms
コンパイル使用メモリ 11,256 KB
実行使用メモリ 16,064 KB
最終ジャッジ日時 2023-09-14 00:10:31
合計ジャッジ時間 4,513 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,228 KB
testcase_01 AC 83 ms
15,064 KB
testcase_02 AC 82 ms
15,144 KB
testcase_03 AC 90 ms
15,192 KB
testcase_04 AC 83 ms
15,116 KB
testcase_05 AC 95 ms
15,364 KB
testcase_06 AC 89 ms
15,228 KB
testcase_07 AC 87 ms
15,132 KB
testcase_08 AC 92 ms
15,532 KB
testcase_09 AC 97 ms
15,792 KB
testcase_10 AC 100 ms
15,988 KB
testcase_11 AC 97 ms
15,640 KB
testcase_12 AC 94 ms
15,500 KB
testcase_13 AC 87 ms
15,288 KB
testcase_14 AC 100 ms
15,744 KB
testcase_15 AC 103 ms
15,924 KB
testcase_16 AC 102 ms
15,944 KB
testcase_17 AC 104 ms
15,960 KB
testcase_18 AC 86 ms
15,136 KB
testcase_19 AC 103 ms
15,860 KB
testcase_20 AC 83 ms
15,168 KB
testcase_21 AC 82 ms
15,156 KB
testcase_22 AC 98 ms
15,676 KB
testcase_23 AC 104 ms
16,064 KB
testcase_24 AC 104 ms
15,880 KB
testcase_25 AC 104 ms
15,984 KB
testcase_26 AC 82 ms
15,044 KB
testcase_27 AC 87 ms
15,212 KB
testcase_28 AC 102 ms
15,948 KB
testcase_29 AC 95 ms
15,696 KB
testcase_30 AC 79 ms
15,064 KB
testcase_31 AC 81 ms
15,148 KB
testcase_32 AC 95 ms
15,800 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:31: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

def main
    
    n = STDIN.readline.chomp.to_i
    queue = [{index: 1, count: 1}]
    visited = []
    visited[1] = true

    while queue.size > 0
        q = queue[0]

        if q[:index] == n
            puts q[:count]
            return
        end

        f = forward(q[:index])
        if f <= n && visited[f].nil?
            queue << {index: f, count: q[:count] + 1}
            visited[f] = true
        end

        b = back(q[:index])
        if b >= 1 && visited[b].nil?
            queue << {index: b, count: q[:count] + 1}
            visited[b] = true
        end

        queue.shift
    end

    puts -1

end

def forward(n)

    n + bit_count(n)

end

def back(n)

    n - bit_count(n)

end

def bit_count(n)

    count = 0
    n.bit_length.times {|i| count += 1 if n[i] == 1}
    count

end

main()

0