結果

問題 No.3 ビットすごろく
ユーザー AIZAWA MasahiroAIZAWA Masahiro
提出日時 2017-02-27 23:31:53
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 997 bytes
コンパイル時間 508 ms
コンパイル使用メモリ 11,576 KB
実行使用メモリ 139,912 KB
最終ジャッジ日時 2023-09-02 13:33:42
合計ジャッジ時間 14,215 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
15,352 KB
testcase_01 AC 82 ms
15,104 KB
testcase_02 AC 82 ms
15,176 KB
testcase_03 AC 143 ms
24,188 KB
testcase_04 AC 91 ms
16,304 KB
testcase_05 AC 319 ms
53,436 KB
testcase_06 AC 156 ms
26,176 KB
testcase_07 AC 110 ms
19,224 KB
testcase_08 AC 239 ms
40,240 KB
testcase_09 AC 472 ms
76,728 KB
testcase_10 AC 602 ms
99,728 KB
testcase_11 AC 398 ms
67,708 KB
testcase_12 AC 305 ms
51,736 KB
testcase_13 AC 127 ms
21,704 KB
testcase_14 AC 580 ms
95,420 KB
testcase_15 AC 867 ms
137,048 KB
testcase_16 AC 742 ms
120,396 KB
testcase_17 AC 812 ms
132,640 KB
testcase_18 AC 116 ms
20,280 KB
testcase_19 AC 880 ms
139,364 KB
testcase_20 AC 88 ms
15,660 KB
testcase_21 AC 83 ms
15,064 KB
testcase_22 AC 591 ms
96,772 KB
testcase_23 AC 886 ms
139,912 KB
testcase_24 AC 882 ms
139,452 KB
testcase_25 AC 863 ms
136,456 KB
testcase_26 WA -
testcase_27 AC 136 ms
23,088 KB
testcase_28 AC 723 ms
116,260 KB
testcase_29 AC 409 ms
69,368 KB
testcase_30 AC 83 ms
15,280 KB
testcase_31 AC 86 ms
15,308 KB
testcase_32 AC 368 ms
61,772 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n = gets.to_i

def to_binary(x)
  binary_str = ''
  until x == 0 || x == 1
    binary_str += (x % 2).to_s
    x /= 2
  end
  binary_str += x.to_s
  binary_str.reverse
end

def get_distance(current)
  to_binary(current).split('').map(&:to_i).inject(:+)
end

def move(histories, n, passed)
  next_histories = histories.map do |history|
    distance = get_distance(history.last)
    [
      history.dup + [history.last + distance],
      history.dup + [history.last - distance]
    ].reject do |new_history|
      next_position = new_history.last
      return new_history if next_position == n

      next true if passed.include?(new_history.last)
      next true if next_position < 1 || next_position > n
      false
    end
  end.flatten(1).uniq {|history| history.last}
  return [] if next_histories.empty?
  passed += next_histories.map(&:last)
  move(next_histories, n, passed)
end

def move_to_goal(n)
  move([[1]], n, [])
end

result = move_to_goal(n)
puts result.empty? ? -1 : result.length

0