結果
問題 | No.1577 織姫と彦星2 |
ユーザー |
|
提出日時 | 2021-07-21 14:46:23 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,061 bytes |
コンパイル時間 | 111 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 19,364 KB |
最終ジャッジ日時 | 2024-07-17 14:01:15 |
合計ジャッジ時間 | 4,088 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | TLE * 1 -- * 52 |
ソースコード
from collections import deque def hamming_distance(a, b): tmp = bin(a ^ b) hd = 0 for i in range(len(tmp)): if tmp[i] == '1': hd += 1 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)