본문 바로가기

Algorithm

5014 스타트링크 / DFS, BFS

 

정답

  • 숨바꼭질이랑 비슷한 문제
  • 하나의 그래프에서 최소거리로 탐색한다고 보면 됨
from collections import deque
f, s, g, u, d = map(int, input().split()) # 건물의 총 수, 현재 위치, 회사 위치, 위로 u층, 아래로 d층

distance = [0] * (f + 1)
visited = [False] * (f + 1)

def BFS(s):
    queue = deque()
    queue.append(s)
    visited[s] = True

    while queue:
        current = queue.popleft()

        if current == g:
            return distance[current]

        for next in (current + u, current - d):
            if 0 < next <= f and not visited[next]:
                queue.append(next)
                visited[next] = True
                distance[next] = distance[current] + 1

    return "use the stairs"

print(BFS(s))

'Algorithm' 카테고리의 다른 글

7562 나이트의 이동 / DFS, BFS  (0) 2025.05.29
1926 그림 / DFS, BFS  (0) 2025.05.27
1246 온라인 판매 / 그리디  (0) 2025.05.26
1302 베스트 셀러 / 해시맵  (0) 2025.05.25
1697 숨바꼭질 / DFS, BFS  (0) 2025.05.24