728x90
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
백준의 숨바꼭질 문제와 굉장히 비슷하다.
그냥 단순히 배열을 만들어서 BFS로 풀면 끝난다.
import sys
from collections import deque
F, S, G, U, D = map(int, sys.stdin.readline().split())
stair = [float('inf') for _ in range(F + 1)]
stair[S] = 0
need_visit = deque()
need_visit.append(S)
while need_visit:
cur_stair = need_visit.popleft()
if cur_stair == G:
break
next_up, next_down = cur_stair + U, cur_stair - D
next_cnt = stair[cur_stair] + 1
if 0 < next_up <= F and stair[next_up] > next_cnt:
stair[next_up] = next_cnt
need_visit.append(next_up)
if 0 < next_down <= F and stair[next_down] > next_cnt:
stair[next_down] = next_cnt
need_visit.append(next_down)
print(stair[G] if stair[G] != float('inf') else "use the stairs")
'알고리즘 > 그래프' 카테고리의 다른 글
백준 1967 트리의 지름 (Python) (0) | 2024.02.18 |
---|---|
백준 16234 인구 이동 (Python) (0) | 2024.02.18 |
백준 1167 트리의 지름 (Python) (0) | 2024.02.18 |
백준 7562 나이트의 이동 (Python) (0) | 2024.02.17 |
백준 2644 촌수계산 (Python) (0) | 2024.02.12 |