Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
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
Archives
Today
Total
관리 메뉴

킹솔이

[백준/Python] 5014 스타트링크 본문

Algorithm

[백준/Python] 5014 스타트링크

킹솔이 2021. 1. 17. 00:03
from _collections import deque

f, s, g, u, d= map(int, input().split())

visited = [0 for _ in range(f+1)]

def bfs():
    que = deque()
    que.append(s)
    visited[s] = 1

    while que:
        floor = que.popleft()
        if floor==g:
            return visited[g]-1
        #up
        if floor+u <= f and not visited[floor+u]: # 조건 주의
            que.append(floor+u)
            visited[floor+u] = visited[floor]+1
        #down
        if floor-d >= 1 and not visited[floor-d]:
            que.append(floor-d)
            visited[floor-d] = visited[floor]+1

    return "use the stairs"

print(bfs())

BFS를 이용하면 된다.

queue는 멀티스레드용 모듈이라 매우 느리고, 싱글스레드용으로는 collections.deque을 쓴다고 한다.

'Algorithm' 카테고리의 다른 글

[백준/Python] 11497 통나무 건너뛰기  (0) 2021.01.18
[백준/Python] 1260 DFS와 BFS  (0) 2021.01.18
[백준/Python] 2170 선 긋기  (0) 2021.01.15
[백준/Python] 13023 ABCDE  (0) 2021.01.13
[백준/Python] 14867 물통  (0) 2021.01.13