結果

問題 No.3 ビットすごろく
ユーザー shi-moshi-mo
提出日時 2016-03-27 23:30:39
言語 Ruby
(3.3.0)
結果
AC  
実行時間 174 ms / 5,000 ms
コード長 495 bytes
コンパイル時間 437 ms
コンパイル使用メモリ 11,308 KB
実行使用メモリ 17,804 KB
最終ジャッジ日時 2023-09-13 23:47:57
合計ジャッジ時間 6,075 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,052 KB
testcase_01 AC 83 ms
15,040 KB
testcase_02 AC 83 ms
15,248 KB
testcase_03 AC 96 ms
16,100 KB
testcase_04 AC 87 ms
15,416 KB
testcase_05 AC 116 ms
16,724 KB
testcase_06 AC 96 ms
16,100 KB
testcase_07 AC 95 ms
15,616 KB
testcase_08 AC 110 ms
16,464 KB
testcase_09 AC 132 ms
16,824 KB
testcase_10 AC 161 ms
17,192 KB
testcase_11 AC 128 ms
16,852 KB
testcase_12 AC 117 ms
16,560 KB
testcase_13 AC 93 ms
15,704 KB
testcase_14 AC 152 ms
17,308 KB
testcase_15 AC 169 ms
17,488 KB
testcase_16 AC 166 ms
17,420 KB
testcase_17 AC 169 ms
17,804 KB
testcase_18 AC 92 ms
15,768 KB
testcase_19 AC 174 ms
17,644 KB
testcase_20 AC 86 ms
15,456 KB
testcase_21 AC 83 ms
15,228 KB
testcase_22 AC 154 ms
17,136 KB
testcase_23 AC 172 ms
17,676 KB
testcase_24 AC 173 ms
17,636 KB
testcase_25 AC 169 ms
17,504 KB
testcase_26 AC 84 ms
15,116 KB
testcase_27 AC 94 ms
15,780 KB
testcase_28 AC 162 ms
17,496 KB
testcase_29 AC 131 ms
16,812 KB
testcase_30 AC 83 ms
15,088 KB
testcase_31 AC 83 ms
15,160 KB
testcase_32 AC 121 ms
16,768 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]

  [i + bitcount(i), i - bitcount(i)].each do |neighbor|
    next if (neighbor < 1) || (n < neighbor)

    nm_neighbor = $nmoves[i] + 1
    next if $nmoves[neighbor] && $nmoves[neighbor] <= nm_neighbor

    $nmoves[neighbor] = nm_neighbor
    move_from(neighbor, n)
  end
end

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