結果

問題 No.1576 織姫と彦星
ユーザー ああいい
提出日時 2021-12-23 19:17:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 239 ms / 2,000 ms
コード長 743 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 82,376 KB
実行使用メモリ 76,856 KB
最終ジャッジ日時 2024-09-17 17:50:07
合計ジャッジ時間 8,463 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
s,e = map(int,input().split())
l = list(map(int,input().split()))
l.insert(0,s)
l.append(e)
C = 10 ** 5
memo = [C] * (N + 2)
memo[0] = 0

def hamu(a,b):
    count = 0
    while a != 0 or b != 0:
        if a & 1 != b & 1:
            count += 1
        a >>= 1
        b >>= 1
    return count < 2
G = [[] for _ in range(N + 2)]
for i in range(N+1):
    for j in range(i + 1,N + 2):
        if hamu(l[i],l[j]):
            G[i].append(j)
            G[j].append(i)
    
from collections import deque
q = deque()
q.append(0)
while len(q):
    now = q.popleft()
    for v in G[now]:
        if memo[v] == C:
            memo[v] = memo[now] + 1
            q.append(v)
if memo[-1] == C:
    print(-1)
else:
    print(memo[-1]-1)
0