結果
| 問題 |
No.3 ビットすごろく
|
| ユーザー |
|
| 提出日時 | 2016-10-30 15:51:29 |
| 言語 | Ruby (3.4.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,089 bytes |
| コンパイル時間 | 274 ms |
| コンパイル使用メモリ | 7,424 KB |
| 実行使用メモリ | 29,568 KB |
| 最終ジャッジ日時 | 2024-11-24 23:37:03 |
| 合計ジャッジ時間 | 66,879 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 WA * 4 RE * 12 TLE * 7 |
コンパイルメッセージ
Syntax OK
ソースコード
def main
n = STDIN.readline.chomp.to_i
table_forward, table_back = init_table(n)
count = sugoroku(n, n, 0, table_forward, table_back)
puts count < n ? count : -1
end
def init_table(n)
table_forward = []
table_back = []
1.upto(n) do |i|
table_forward[i] = i + bitcount(i)
table_back[i] = i - bitcount(i)
end
[table_forward, table_back]
end
def sugoroku(n, goal, count, table_forward, table_back)
ret = n
if count >= n
return ret
elsif goal == 1
return count
end
1.upto(goal - 1) do |i|
if table_forward[i] == goal
ret_tmp = sugoroku(n, i, count + 1, table_forward, table_back)
ret = ret_tmp if ret_tmp < ret
end
end
n.downto(goal + 1) do |i|
if table_back[i] == goal
ret_tmp = sugoroku(n, i, count + 1, table_forward, table_back)
ret = ret_tmp if ret_tmp < ret
end
end
ret
end
def bitcount(n)
count = 0
n.bit_length.times {|i| count += 1 if n[i] == 1}
count
end
main()