BFS - 5014번: 스타트링크

jisu_log·2025년 8월 4일

알고리즘 문제풀이

목록 보기
63/105


from collections import deque


line = list(map(int, input().split()))

F, S, G, U, D = line[0], line[1], line[2], line[3], line[4]

def bfs(F, S, G, U, D):
    q = deque()
    visited = set()
    q.append((S, 0))
    visited.add(S)
    while q:
        n, turn = q.popleft()

        if n == G:
            return turn

        if n - D > 0 and n - D not in visited:
            q.append((n - D, turn + 1))
            visited.add(n - D)
        if n + U <= F and n + U not in visited:
            q.append((n + U, turn + 1))
            visited.add(n + U)
    return -1

res = bfs(F, S, G, U, D)

if res == -1:
    print('use the stairs')
else: 
    print(res)

0개의 댓글