結果

問題 No.3 ビットすごろく
ユーザー shi-moshi-mo
提出日時 2016-03-28 13:20:05
言語 Ruby
(3.3.0)
結果
AC  
実行時間 138 ms / 5,000 ms
コード長 458 bytes
コンパイル時間 143 ms
コンパイル使用メモリ 11,392 KB
実行使用メモリ 16,016 KB
最終ジャッジ日時 2023-09-13 23:48:18
合計ジャッジ時間 5,077 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
15,260 KB
testcase_01 AC 83 ms
15,132 KB
testcase_02 AC 85 ms
15,176 KB
testcase_03 AC 90 ms
15,440 KB
testcase_04 AC 85 ms
15,192 KB
testcase_05 AC 107 ms
15,464 KB
testcase_06 AC 92 ms
15,336 KB
testcase_07 AC 88 ms
15,308 KB
testcase_08 AC 101 ms
15,516 KB
testcase_09 AC 113 ms
15,588 KB
testcase_10 AC 129 ms
15,804 KB
testcase_11 AC 112 ms
15,500 KB
testcase_12 AC 105 ms
15,424 KB
testcase_13 AC 90 ms
15,196 KB
testcase_14 AC 126 ms
15,844 KB
testcase_15 AC 137 ms
15,792 KB
testcase_16 AC 133 ms
15,768 KB
testcase_17 AC 135 ms
15,988 KB
testcase_18 AC 90 ms
15,372 KB
testcase_19 AC 136 ms
15,856 KB
testcase_20 AC 83 ms
15,128 KB
testcase_21 AC 82 ms
15,116 KB
testcase_22 AC 126 ms
15,860 KB
testcase_23 AC 136 ms
16,004 KB
testcase_24 AC 136 ms
16,016 KB
testcase_25 AC 138 ms
15,896 KB
testcase_26 AC 82 ms
15,256 KB
testcase_27 AC 91 ms
15,348 KB
testcase_28 AC 132 ms
15,828 KB
testcase_29 AC 114 ms
15,836 KB
testcase_30 AC 85 ms
15,152 KB
testcase_31 AC 83 ms
15,120 KB
testcase_32 AC 108 ms
15,440 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

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]

  move(i, i + bitcount(i), n)
  move(i, i - bitcount(i), n)
end

def move(from, to, n)
  return if (to < 1) || (n < to)
  
  nm = $nmoves[from] + 1
  return if $nmoves[to] && $nmoves[to] <= nm
  
  $nmoves[to] = nm
  move_from(to, n)
end

$nmoves[1] = 1
1.upto(n-1) do |i|
  move_from(i, n)
end
puts $nmoves[n] || -1
0