🗝️ Algorithm/🟩 백준

🟩 [백준] [Python] [BFS/DFS] 5014번_스타트링크

Dbswnstjd 2022. 11. 17. 02:29

문제

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

 

5014번: 스타트링크

첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.

www.acmicpc.net

풀이

# 백준 5014번 문제 - 스타트링크
from collections import deque
import sys
input = sys.stdin.readline

def bfs(v):
    q = deque([v])
    visited[v] = 1
    while q:
        v = q.popleft()
        if v == g:
            return count[g]
        for i in (v+u, v-d):
            if 0 < i <= f and not visited[i]:
                visited[i] = 1
                count[i] = count[v] + 1
                q.append(i)
    if count[g] == 0:
        return 'use the stairs'

f, s, g, u, d = map(int, input().split())
visited = [0 for i in range(f+1)]
count = [0 for i in range(f+1)]
print(bfs(s))