[Baekjoon] 5014 스타트링크 python

sorzzzzy·2021년 8월 4일
0

Baekjoon Algorithm

목록 보기
30/46
post-thumbnail

🏷 문제


💡 코드

from sys import stdin
from collections import deque

def find(s,g):
    q = deque()
    q.append(s)
    # 현재 있는 위치에서는 버튼을 안 눌러도 되므로
    visited[s] = 0
    # 이동할 수 있는 경우
    case = [u, -d]
    while q:
        x = q.popleft()
        if x == g:
            return visited[x]
        for i in range(2):
            newx = x + case[i]
            if 0 < newx <= f and visited[newx] == -1:
                # 버튼을 한번 누름 (+1)
                visited[newx] = visited[x]+1
                q.append(newx)

    return 'use the stairs'

f, s, g, u, d = map(int, stdin.readline().split())
# 방문확인 겸 버튼 누른 횟수 알기 위한 용도
visited = [-1] * (f+1)

res = find(s,g)
print(res)

🔑

profile
Backend Developer

0개의 댓글