Untitled
unknown
plain_text
2 years ago
540 B
14
Indexable
from queue import Queue
def min_flights(N, a, b):
graph = defaultdict(lambda: [])
for i in range(1, n+1):
if 2*i <= n:
graph[i].append(2*i)
if i-1 >= 0:
graph[i].append(i-1)
queue = Queue()
queue.put((a, 0))
visited = {a}
while not queue.empty():
u, level = queue.get()
if u == b:
return level
for v in graph[u]:
if v not in visited:
visited.add(v)
queue.put((v, level+1))
return -1
Editor is loading...