結果

問題 No.3 ビットすごろく
ユーザー tshigitshigi
提出日時 2016-08-01 11:52:23
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 684 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 85,988 KB
最終ジャッジ日時 2024-04-24 13:28:25
合計ジャッジ時間 10,248 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
12,288 KB
testcase_01 AC 92 ms
12,160 KB
testcase_02 AC 91 ms
12,288 KB
testcase_03 AC 2,833 ms
53,060 KB
testcase_04 AC 258 ms
20,608 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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)]]
  until queue.empty?
    path = queue.shift
    cands = path.last.next_cands.reject { |cand| (queue + path).flatten.include?(cand) }
    return path.size + 1 if cands.any?(&:goal?)
    queue.push(*cands.map { |cand| path + [cand] })
  end
  -1
end

puts search
0