結果

問題 No.1577 織姫と彦星2
ユーザー k1832
提出日時 2021-06-19 00:22:59
言語 Python3
(3.14.3 + numpy 2.4.2 + scipy 1.17.0)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 777 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 336 ms
コンパイル使用メモリ 20,952 KB
実行使用メモリ 24,252 KB
最終ジャッジ日時 2026-03-07 04:38:09
合計ジャッジ時間 11,711 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import random
import queue
def solve(start, end, stones):
    stone_set = set(stones)
    checked_set = set()
    que = queue.Queue()
    que.put((start, 0))
    while not que.empty():
        current, distance = que.get()
        next_distance = distance + 1
        for i in range(31):
            candidate = current ^ (1<<i)
            if candidate == end:
                return next_distance - 1
            
            if candidate in stone_set and candidate not in checked_set:
                checked_set.add(candidate)
                que.put((candidate, next_distance))
    return -1

if __name__ == '__main__':
    N = int(input())
    start, end = list(map(int, input().split()))
    stones = list(map(int, input().split()))
    print(solve(start, end, stones))
0