結果

問題 No.1577 織姫と彦星2
ユーザー yuruhiya
提出日時 2021-06-29 21:00:58
言語 Crystal
(1.14.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 431 bytes
コンパイル時間 16,867 ms
コンパイル使用メモリ 295,012 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-06-25 23:27:04
合計ジャッジ時間 13,614 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

n = read_line.to_i
s, t = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
stones = a + [s, t]
hash = stones.zip(0..).to_h
que = Deque{hash[s]}
dist = [nil.as(Int32?)] * (n + 2)
dist[hash[s]] = -1
while i = que.shift?
  d = dist[i].not_nil!
  puts(d) + exit if i == hash[t]
  (0..30).each do |bit|
    if (j = hash[stones[i] ^ (1 << bit)]?) && dist[j].nil?
      dist[j] = d + 1
      que << j
    end
  end
end
puts -1
0