結果

問題 No.1577 織姫と彦星2
ユーザー mokan0mokan0
提出日時 2021-07-21 15:03:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,149 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 11,092 KB
実行使用メモリ 15,180 KB
最終ジャッジ日時 2023-09-24 13:09:16
合計ジャッジ時間 4,058 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
13,052 KB
testcase_01 AC 19 ms
8,664 KB
testcase_02 AC 20 ms
8,764 KB
testcase_03 AC 20 ms
8,640 KB
testcase_04 AC 19 ms
8,744 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
権限があれば一括ダウンロードができます

ソースコード

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