結果

問題 No.1577 織姫と彦星2
ユーザー kitatike55
提出日時 2021-07-08 23:25:00
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,104 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 20,336 KB
最終ジャッジ日時 2024-07-01 13:15:24
合計ジャッジ時間 9,909 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
start,end = map(int,input().split())
stone = list(map(int,input().split()))
stoneMax = max(stone)
maxLen = int(len(bin(stoneMax)[2:]))

setStone= set(stone)
avoidStone = set()
from collections import deque
def bfs(u):
    queue = deque([u,0])
    while queue:
        v = queue.popleft()
        dis = queue.popleft()
        h = serchHam(v)
        if end in h:
            print(dis)
            exit()
        # stoneS = stone.copy()
        for i in h:
            # if i in setStone:
            queue.append(i)
            queue.append(dis+1)
                # stone.remove(i)
    print(-1)
    exit() 
      
def serchHam(a):
    h=[]
    abin = bin(a+2**maxLen)[2:]
    for i in range(1,maxLen+1):
        if abin[i] == '0':
            hh = 2**(maxLen-i)+a
        else:
            hh = a-2**(maxLen-i)
        if hh == end:
            h.append(hh)
            return h
        if hh in setStone:
            if hh in avoidStone:
                continue
            else:
                h.append(hh)
                avoidStone.add(hh)
    # print(h)
    return h

bfs(start)
0