結果

問題 No.3 ビットすごろく
ユーザー tshigi
提出日時 2016-08-01 11:52:23
言語 Ruby
(3.4.1)
結果
TLE  
実行時間 -
コード長 684 bytes
コンパイル時間 58 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 84,168 KB
最終ジャッジ日時 2024-11-06 21:38:08
合計ジャッジ時間 10,374 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 5 TLE * 1 -- * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
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