Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

짱이 될거야

백준 5014: 스타트링크 Python (BFS) 본문

알고리즘

백준 5014: 스타트링크 Python (BFS)

jeong57 2022. 11. 29. 10:11

 

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))
Comments