BOJ - 5014 - 스타트링크

Updated:

import sys
from collections import deque

def BFS(F, S, G, U, D):
    visit = [False] * (F + 1)
    dq = deque()
    dq.append((S, 0))
    visit[S] = True

    while dq:
        vertex, weight = dq.popleft()
        if vertex == G:
            return weight

        
        if vertex - D > 0:
            if visit[vertex - D] == False:
                dq.append((vertex - D, weight + 1))
                visit[vertex - D] = True

        if vertex + U <= F:
            if visit[vertex + U] == False: 
                dq.append((vertex + U, weight + 1))
                visit[vertex + U] = True

    return -1

def solution():
    F, S, G, U, D = map(int, sys.stdin.readline().split())
    ret = BFS(F, S, G, U, D)
    print(ret if ret != -1 else 'use the stairs')

solution()

https://www.acmicpc.net/problem/5014

Categories:

Updated:

Leave a comment