結果

問題 No.3 ビットすごろく
ユーザー tshigitshigi
提出日時 2016-08-01 12:15:40
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 753 bytes
コンパイル時間 57 ms
コンパイル使用メモリ 11,580 KB
実行使用メモリ 27,280 KB
最終ジャッジ日時 2023-08-06 18:41:54
合計ジャッジ時間 4,815 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
15,108 KB
testcase_01 AC 79 ms
15,148 KB
testcase_02 AC 79 ms
15,168 KB
testcase_03 AC 88 ms
18,640 KB
testcase_04 AC 82 ms
15,516 KB
testcase_05 AC 103 ms
21,136 KB
testcase_06 AC 91 ms
18,900 KB
testcase_07 AC 87 ms
16,804 KB
testcase_08 AC 95 ms
19,764 KB
testcase_09 AC 110 ms
22,688 KB
testcase_10 AC 113 ms
23,924 KB
testcase_11 AC 105 ms
22,068 KB
testcase_12 AC 100 ms
20,828 KB
testcase_13 AC 87 ms
18,056 KB
testcase_14 AC 111 ms
23,720 KB
testcase_15 AC 121 ms
27,280 KB
testcase_16 AC 117 ms
25,828 KB
testcase_17 AC 119 ms
27,228 KB
testcase_18 AC 87 ms
17,300 KB
testcase_19 AC 122 ms
26,828 KB
testcase_20 AC 85 ms
15,460 KB
testcase_21 AC 81 ms
15,276 KB
testcase_22 AC 116 ms
23,636 KB
testcase_23 AC 124 ms
27,088 KB
testcase_24 AC 127 ms
26,852 KB
testcase_25 AC 125 ms
27,080 KB
testcase_26 WA -
testcase_27 AC 89 ms
18,288 KB
testcase_28 AC 121 ms
25,684 KB
testcase_29 AC 106 ms
22,336 KB
testcase_30 AC 81 ms
15,324 KB
testcase_31 AC 81 ms
15,308 KB
testcase_32 AC 106 ms
21,860 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