Algorithm/Python

[백준 5014] 스타트링크

🥭맹2 2021. 6. 5. 20:24

1. 문제

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

2. 접근 방법

bfs를 사용하면 됩니다.

3. 코드

python

from collections import deque


def bfs():
    global F, S, G, U, D, visited
    visited = [False] * 1000001
    q = deque()
    visited[S] = True
    q.append((S, 0))

    while q:
        now, cnt = q.popleft()
        if now == G:
            return cnt
        if now + U <= F and not visited[now+U]:
            q.append((now+U, cnt+1))
            visited[now+U] = True
        if now - D >= 1 and not visited[now-D]:
            q.append((now-D, cnt+1))
            visited[now-D] = True
    return "use the stairs"


F, S, G, U, D = map(int, input().split())
print(bfs())

4. 마치며

마치며~!

'Algorithm > Python' 카테고리의 다른 글

[백준 2644] 촌수계산  (0) 2021.06.05
[백준 2573] 빙산  (0) 2021.06.05
[백준 2178] 미로 탐색  (0) 2021.06.05
[백준 2146] 다리 만들기  (0) 2021.06.05
[LeetCode] 234. Palindrome Linked List  (0) 2021.06.05