짱이 될거야
백준 5014: 스타트링크 Python (BFS) 본문
from collections import deque
def bfs(v):
q = deque([v])
visited[v] = 1
while q:
v = q.popleft()
if v == G:
return count[G]
for w in (v+U, v-D):
if 0 < w <= F and not visited[w]:
visited[w] = 1
q.append(w)
count[w] = count[v] + 1
if count[G] == 0:
return "use the stairs"
# F: 총 층수, S: 현재 층, G: 목적지
# U: 위 버튼, D: 아래 버튼
F, S, G, U, D = map(int, input().split())
visited = [0] * (F+1) # 방문체크
count = [0] * (F+1)
print(bfs(S))
'알고리즘' 카테고리의 다른 글
백준 1158: 요세푸스 문제 Python (0) | 2022.12.01 |
---|---|
백준 20920: 영단어 암기는 괴로워 Python (0) | 2022.11.29 |
백준 7569: 토마토 Python (BFS) (0) | 2022.11.28 |
백준 3009: 네 번째 점 Python (0) | 2022.11.25 |
백준 25501: 재귀의 귀재 Python (0) | 2022.11.25 |
Comments