結果

問題 No.1576 織姫と彦星
ユーザー m t
提出日時 2021-07-21 13:04:10
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 1,130 ms / 2,000 ms
コード長 731 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 417 ms
コンパイル使用メモリ 20,700 KB
実行使用メモリ 15,484 KB
最終ジャッジ日時 2026-04-01 04:58:09
合計ジャッジ時間 27,724 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
def BFS(s,N):
    dist=[False]*N
    que=deque([0])
    dist[0]=0
    while que:
        v=que.popleft()
        for u in graph[v]:
            if not dist[u]:
                dist[u]=dist[v]+1
                que.append(u)
    return dist[-1]

def hamming(a,b):
    x=bin(a^b)
    d=0
    for i in x[2:]:
        d+=int(i)
    return d

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

x=[start]+stone+[end]
graph=[[] for i in range(n+2)]
for i in range(n+1):
    for j in range(i+1,n+2):
        if hamming(x[i],x[j])==1:
            graph[i].append(j)
            graph[j].append(i)
            
res=BFS(0,n+2)
if not res:
    print(-1)
else:
    print(res-1)
0