結果

問題 No.1577 織姫と彦星2
ユーザー mokan0mokan0
提出日時 2021-07-21 15:03:14
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,149 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 17,952 KB
最終ジャッジ日時 2024-07-17 14:01:51
合計ジャッジ時間 4,195 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other TLE * 1 -- * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def hamming_distance(a, b):
    tmp = bin(a ^ b)
    hd = 0
    for i in range(len(tmp)):
        if hd == 0 and tmp[i] == '1':
            hd = 1
        elif hd == 1 and tmp[i] == '1':
            hd = 2
            break
    return hd


N = int(input())
start, end = map(int, input().split())
stone = list(map(int, input().split()))
#print(N, start, end, stone)

all_stone = [start]+stone+[end]
#print(hamming_distance(start, end), all_stone)
g = [[] for _ in range(N+2)]  # 隣接リスト
for i in range(len(all_stone)-1):
    for j in range(i+1, len(all_stone)):
        dis = hamming_distance(all_stone[i], all_stone[j])
        if dis == 1:
            g[i].append(j)
            g[j].append(i)
# print(g)


def bfs(u):
    queue = deque([u])
    d = [None]*len(all_stone)  # uからの距離の初期化
    #print(queue, d)
    d[u] = 0
    while queue:
        v = queue.popleft()
        for i in g[v]:
            if d[i] is None:
                d[i] = d[v]+1
                queue.append(i)
    return d


d = bfs(0)
if d[len(all_stone)-1] is None:
    print(-1)
else:
    print(d[len(all_stone)-1]-1)
0