結果

問題 No.3 ビットすごろく
ユーザー Shintani TeppeiShintani Teppei
提出日時 2017-10-31 17:20:25
言語 Ruby
(3.3.0)
結果
AC  
実行時間 97 ms / 5,000 ms
コード長 716 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 11,460 KB
実行使用メモリ 16,948 KB
最終ジャッジ日時 2023-09-14 00:50:29
合計ジャッジ時間 4,433 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
15,444 KB
testcase_01 AC 82 ms
15,348 KB
testcase_02 AC 81 ms
15,260 KB
testcase_03 AC 87 ms
15,356 KB
testcase_04 AC 88 ms
15,164 KB
testcase_05 AC 89 ms
16,112 KB
testcase_06 AC 89 ms
15,420 KB
testcase_07 AC 85 ms
15,348 KB
testcase_08 AC 87 ms
15,584 KB
testcase_09 AC 92 ms
16,284 KB
testcase_10 AC 93 ms
16,396 KB
testcase_11 AC 90 ms
16,188 KB
testcase_12 AC 89 ms
15,960 KB
testcase_13 AC 84 ms
15,520 KB
testcase_14 AC 93 ms
16,332 KB
testcase_15 AC 96 ms
16,712 KB
testcase_16 AC 94 ms
16,636 KB
testcase_17 AC 93 ms
16,736 KB
testcase_18 AC 84 ms
15,428 KB
testcase_19 AC 94 ms
16,796 KB
testcase_20 AC 84 ms
15,296 KB
testcase_21 AC 81 ms
15,044 KB
testcase_22 AC 93 ms
16,464 KB
testcase_23 AC 97 ms
16,948 KB
testcase_24 AC 97 ms
16,832 KB
testcase_25 AC 96 ms
16,784 KB
testcase_26 AC 83 ms
15,172 KB
testcase_27 AC 87 ms
15,520 KB
testcase_28 AC 96 ms
16,556 KB
testcase_29 AC 92 ms
16,276 KB
testcase_30 AC 83 ms
15,152 KB
testcase_31 AC 84 ms
15,252 KB
testcase_32 AC 92 ms
16,124 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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