結果

問題 No.1576 織姫と彦星
ユーザー boeboeMartin
提出日時 2021-07-21 14:36:14
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 243 ms / 2,000 ms
コード長 1,058 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 214 ms
コンパイル使用メモリ 85,120 KB
実行使用メモリ 81,408 KB
最終ジャッジ日時 2026-04-01 05:00:16
合計ジャッジ時間 6,680 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
# import time

def hmg_d(a, b):
    x = bin(a ^ b)
    cnt = 0
    for i in x:
        if i == "1":
            cnt += 1
        if cnt > 1:
            return False
    if cnt == 1:
        return True
    else:
        return False

n = int(input())
start, end = map(int,input().split())
stone = list(map(int,input().split()))

# start_time = time.time()

if hmg_d(start, end):
    print(0)
    exit()

stone_dict = {}
stone_dict[start] = 0
end_dict = {}
for i in stone:
    stone_dict[i] = 0
    end_dict[i] = 0
    if hmg_d(i, end):
        end_dict[i] = 1

queue = deque()
queue.append(start)

while queue:
    v = queue.popleft()
    l = []
    for i in stone:
        if hmg_d(v, i):
            stone_dict[i] = stone_dict[v] + 1
            queue.append(i)

            # 終了条件
            if end_dict[i] == 1:
                print(stone_dict[i])
                # print(time.time() - start_time)
                exit()
            
            l.append(i)
    
    for i in l:
        stone.remove(i)

print(-1)
0