結果

問題 No.3 ビットすごろく
コンテスト
ユーザー iceEiji2
提出日時 2016-11-22 15:14:39
言語 Ruby
(4.0.2)
コンパイル:
ruby -w -c _filename_
実行:
ruby _filename_
結果
AC  
実行時間 55 ms / 5,000 ms
+ 809µs
コード長 623 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 36 ms
コンパイル使用メモリ 8,832 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2026-08-03 03:48:15
合計ジャッジ時間 3,399 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #
raw source code

N = gets.to_i

result = -1
queue = [{pos: 1, time: 1}]
check = Array.new(N+1, false)
check[1] = true

while queue.length > 0 do
  current = queue.shift
  if current[:pos] == N
    result = current[:time]
    break
  end
  move = current[:pos].to_s(2).count("1")
  if current[:pos] - move > 0 && !check[current[:pos] - move]
    queue << {pos: current[:pos] - move, time: current[:time] + 1}
    check[current[:pos] - move] = true
  end
  if current[:pos] + move <= N && !check[current[:pos] + move]
    queue << {pos: current[:pos] + move, time: current[:time] + 1}
    check[current[:pos] + move] = true
  end
end
p result
0