結果

問題 No.3 ビットすごろく
ユーザー Shintani Teppei
提出日時 2017-10-31 17:20:25
言語 Ruby
(3.4.1)
結果
AC  
実行時間 108 ms / 5,000 ms
コード長 716 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 7,680 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-07-01 08:50:25
合計ジャッジ時間 4,425 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:30: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Syntax OK

ソースコード

diff #

n = gets.to_i
arr = (1..n).to_a.map { |i| i.digits(2).count(1) }
INF = 10000000
ans = [INF] * n
ans[0] = 1

que = []
# queueの中身は[現在のマス, 移動数]
que.unshift([0, 1])

while !que.empty?
    index, move_count = que.pop()
    
    if index == n-1
        puts move_count
        exit 0
    end
    
    if index + arr[index] <= n - 1 && move_count + 1 < ans[index + arr[index]]
        que.unshift([index + arr[index], move_count + 1])
        ans[index + arr[index]] = move_count + 1
    end
    
    if index - arr[index] >= 0 && move_count + 1 < ans[index - arr[index]]
        que.unshift([index - arr[index], move_count + 1])
        ans[index + arr[index]] = move_count + 1
    end
end

puts -1
0