https://www.acmicpc.net/problem/5014
이제는 어느 정도 bfs의 감을 잡은 것 같다. 그리고 중요한 것은 역시나 예외 상황 처리가 가장 중요하다.
간단하지만 if의 조건을 쓸 때 앞 쪽에 예외처리를 해줘야하고 불가능한 수들을 다양하게 생각해야 한다.
from collections import deque
f, s, g, u, d = map(int,input().split())
answer = -1
arr = [0 for i in range(1000002)]
q = deque([])
q.append([s,0])
arr[s] = 1
while q:
x, cnt = q.popleft()
if x == g:
answer = cnt
break
if x-d >0 and arr[x-d] == 0:
q.append([x-d,cnt+1])
arr[x-d] = 1
if x+u <= f and arr[x+u] == 0:
q.append([x+u,cnt+1])
arr[x+u] = 1
if answer == -1:
print("use the stairs")
else:
print(answer)
'알고리즘' 카테고리의 다른 글
[백준] 3190번: 뱀 (0) | 2022.04.30 |
---|---|
[백준] 13460: 구슬 탈출 2 (0) | 2022.04.29 |
[백준] 1697번: 숨바꼭질 (0) | 2022.03.22 |
[백준] 2644번: 촌수계산 (0) | 2022.03.22 |
[백준] 2667번: 단지번호붙이기 (0) | 2022.02.02 |