結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-03-27 23:30:39 |
| 言語 | Ruby (4.0.2) |
| 結果 |
AC
|
| 実行時間 | 116 ms / 5,000 ms |
| コード長 | 495 bytes |
| 記録 | |
| コンパイル時間 | 48 ms |
| コンパイル使用メモリ | 9,088 KB |
| 実行使用メモリ | 17,408 KB |
| 最終ジャッジ日時 | 2026-03-22 04:04:54 |
| 合計ジャッジ時間 | 3,571 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
コンパイルメッセージ
Syntax OK
ソースコード
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]
[i + bitcount(i), i - bitcount(i)].each do |neighbor|
next if (neighbor < 1) || (n < neighbor)
nm_neighbor = $nmoves[i] + 1
next if $nmoves[neighbor] && $nmoves[neighbor] <= nm_neighbor
$nmoves[neighbor] = nm_neighbor
move_from(neighbor, n)
end
end
$nmoves[1] = 1
1.upto(n-1) do |i|
move_from(i, n)
end
puts $nmoves[n] || -1