結果

問題 No.3 ビットすごろく
ユーザー tshigitshigi
提出日時 2016-08-01 12:15:40
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 753 bytes
コンパイル時間 118 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 18,816 KB
最終ジャッジ日時 2024-04-24 13:28:31
合計ジャッジ時間 5,081 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
12,032 KB
testcase_01 AC 89 ms
12,288 KB
testcase_02 AC 94 ms
12,160 KB
testcase_03 AC 103 ms
13,952 KB
testcase_04 AC 98 ms
12,288 KB
testcase_05 AC 113 ms
16,128 KB
testcase_06 AC 104 ms
13,952 KB
testcase_07 AC 99 ms
13,568 KB
testcase_08 AC 110 ms
15,232 KB
testcase_09 AC 122 ms
17,152 KB
testcase_10 AC 125 ms
18,048 KB
testcase_11 AC 113 ms
16,768 KB
testcase_12 AC 110 ms
16,000 KB
testcase_13 AC 99 ms
13,696 KB
testcase_14 AC 122 ms
17,536 KB
testcase_15 AC 132 ms
18,816 KB
testcase_16 AC 127 ms
18,432 KB
testcase_17 AC 131 ms
18,816 KB
testcase_18 AC 97 ms
13,312 KB
testcase_19 AC 129 ms
18,816 KB
testcase_20 AC 92 ms
12,288 KB
testcase_21 AC 88 ms
12,032 KB
testcase_22 AC 126 ms
17,664 KB
testcase_23 AC 134 ms
18,816 KB
testcase_24 AC 129 ms
18,560 KB
testcase_25 AC 135 ms
18,816 KB
testcase_26 WA -
testcase_27 AC 102 ms
14,080 KB
testcase_28 AC 129 ms
18,560 KB
testcase_29 AC 118 ms
17,024 KB
testcase_30 AC 90 ms
12,288 KB
testcase_31 AC 92 ms
12,032 KB
testcase_32 AC 117 ms
16,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

inputs = STDIN.readlines.map(&:chomp)
N = inputs[0].to_i

class Koma
  def initialize(pos)
    @pos = pos
  end

  attr_reader :pos

  def ==(other)
    @pos == other.pos
  end

  def valid?
    1 <= @pos && @pos <= N
  end

  def goal?
    @pos == N
  end

  def next_cands
    ons = @pos.to_s(2).count('1')
    [Koma.new(@pos + ons), Koma.new(@pos - ons)].select(&:valid?)
  end
end

def search
  queue = [[Koma.new(1)]]
  visited = { 1 => true }
  until queue.empty?
    path = queue.shift
    cands = path.last.next_cands.reject { |cand| visited[cand.pos] }
    return path.size + 1 if cands.any?(&:goal?)
    queue.push(*cands.map { |cand| path + [cand] })
    visited.update(cands.map { |cand| [cand.pos, true] }.to_h)
  end
  -1
end

puts search
0