結果

問題 No.3 ビットすごろく
ユーザー AIZAWA MasahiroAIZAWA Masahiro
提出日時 2017-02-27 23:40:00
言語 Ruby
(3.3.0)
結果
AC  
実行時間 906 ms / 5,000 ms
コード長 1,020 bytes
コンパイル時間 49 ms
コンパイル使用メモリ 11,296 KB
実行使用メモリ 139,768 KB
最終ジャッジ日時 2023-09-14 00:34:18
合計ジャッジ時間 14,289 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
15,352 KB
testcase_01 AC 80 ms
15,288 KB
testcase_02 AC 80 ms
15,140 KB
testcase_03 AC 140 ms
24,112 KB
testcase_04 AC 92 ms
16,212 KB
testcase_05 AC 329 ms
53,744 KB
testcase_06 AC 156 ms
26,164 KB
testcase_07 AC 109 ms
19,360 KB
testcase_08 AC 238 ms
40,308 KB
testcase_09 AC 469 ms
76,896 KB
testcase_10 AC 610 ms
99,940 KB
testcase_11 AC 408 ms
67,916 KB
testcase_12 AC 314 ms
51,732 KB
testcase_13 AC 128 ms
21,684 KB
testcase_14 AC 576 ms
95,264 KB
testcase_15 AC 867 ms
136,308 KB
testcase_16 AC 743 ms
120,156 KB
testcase_17 AC 816 ms
132,764 KB
testcase_18 AC 118 ms
20,264 KB
testcase_19 AC 880 ms
139,140 KB
testcase_20 AC 88 ms
15,680 KB
testcase_21 AC 81 ms
15,236 KB
testcase_22 AC 589 ms
96,732 KB
testcase_23 AC 881 ms
139,768 KB
testcase_24 AC 906 ms
139,572 KB
testcase_25 AC 868 ms
136,508 KB
testcase_26 AC 82 ms
15,096 KB
testcase_27 AC 135 ms
23,024 KB
testcase_28 AC 725 ms
116,204 KB
testcase_29 AC 416 ms
69,420 KB
testcase_30 AC 84 ms
15,056 KB
testcase_31 AC 86 ms
15,456 KB
testcase_32 AC 372 ms
61,804 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)
  return [1] if n == 1
  move([[1]], n, [])
end

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

0