-
[백준] 5014번 스타트링크 (by Python)Programming/Algorithm 2021. 2. 7. 18:04
문제
변수가 많았지만, 문제를 제대로 이해하기만 한다면 간단하게 BFS로 해결할 수 있는 문제이다. 개인적으로 각 변수를 필요한 자리에 놓는 과정이 즐거웠던 문제이다.
풀이
from collections import deque INF = int(1e9) f, s, g, u, d = map(int, input().split()) distance = [INF] * (f + 1) distance[s] = 0 q = deque([s]) while True: if not q: print('use the stairs') break temp = q.popleft() if temp == g: print(distance[temp]) break else: if 0 < (temp + u) <= f: if distance[temp + u] == INF: q.append(temp + u) distance[temp + u] = distance[temp] + 1 if 0 < (temp - d) <= f: if distance[temp - d] == INF: q.append(temp - d) distance[temp - d] = distance[temp] + 1
기본 BFS 틀에서 요구한 부분만 약간 변형해주는 식으로 코드를 짰다.
'Programming > Algorithm' 카테고리의 다른 글
[백준] 13549번 숨바꼭질 3 (by Python) (0) 2021.02.10 [백준] 1261번 알고스팟 (by Python) (0) 2021.02.10 [백준] 1700번 멀티탭 스케줄링 (by Python) (0) 2021.02.07 [백준] 14226번 이모티콘 (by Python) (0) 2021.02.07 [백준] 1707번 이분 그래프 (by Python) (0) 2021.02.06