結果

問題 No.3 ビットすごろく
ユーザー shi-mo
提出日時 2016-03-28 13:20:05
言語 Ruby
(3.4.1)
結果
AC  
実行時間 149 ms / 5,000 ms
コード長 458 bytes
コンパイル時間 38 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 12,928 KB
最終ジャッジ日時 2024-07-01 07:48:25
合計ジャッジ時間 4,819 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

$nmoves = []
$bc = []

def bitcount(i)
  $bc[i] ||= i.to_s(2).count('1')
end

def move_from(i, n)
  return unless $nmoves[i]

  move(i, i + bitcount(i), n)
  move(i, i - bitcount(i), n)
end

def move(from, to, n)
  return if (to < 1) || (n < to)
  
  nm = $nmoves[from] + 1
  return if $nmoves[to] && $nmoves[to] <= nm
  
  $nmoves[to] = nm
  move_from(to, n)
end

$nmoves[1] = 1
1.upto(n-1) do |i|
  move_from(i, n)
end
puts $nmoves[n] || -1
0