結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2016-10-30 16:07:06 |
言語 | Ruby (3.4.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,089 bytes |
コンパイル時間 | 278 ms |
コンパイル使用メモリ | 7,680 KB |
実行使用メモリ | 29,696 KB |
最終ジャッジ日時 | 2024-11-24 23:38:47 |
合計ジャッジ時間 | 69,726 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 WA * 2 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, 1, 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()