1. 문제
https://www.acmicpc.net/problem/5014
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 |