Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
540 B
6
Indexable
Never
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