結果

問題 No.1577 織姫と彦星2
ユーザー yuruhiya
提出日時 2021-06-29 23:01:05
言語 Crystal
(1.14.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 528 bytes
コンパイル時間 14,394 ms
コンパイル使用メモリ 294,372 KB
実行使用メモリ 6,912 KB
最終ジャッジ日時 2024-06-26 01:40:14
合計ジャッジ時間 15,393 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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)
a << s
a << t

hash = Hash(Int32, Int32).new(n)
n.times do |i|
  hash[a.unsafe_fetch(i)] = i
end
hash[s] = n
hash[t] = n &+ 1

que = Deque{n}
dist = Array(Int32?).new(n &+ 2, nil)
dist[n] = -1
while i = que.shift?
  d = dist[i].not_nil!
  ai = a.unsafe_fetch(i)
  puts(d) + exit if i == n &+ 1
  bit = 1
  30.times do 
    if (j = hash[ai ^ bit]?) && dist[j].nil?
      dist[j] = d &+ 1
      que << j
    end
    bit <<= 1
  end
end
puts -1
0