반응형
https://www.acmicpc.net/problem/5014
5014번: 스타트링크
문제 강호는 코딩 교육을 하는 스타트업 스타트링크에 지원했다. 오늘은 강호의 면접날이다. 하지만, 늦잠을 잔 강호는 스타트링크가 있는 건물에 늦게 도착하고 말았다. 스타트링크는 총 F층으
www.acmicpc.net


BFS로 해결할 수 있는 문제.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
# F : 건물의 총 높이 | |
# G : 목적지 | |
# U : 위로 한 번에 이동할 수 있는 폭 | |
# D : 아래로 한 번에 이동할 수 있는 폭 | |
# 현재위치 = S. | |
F, S, G, U, D = map(int, sys.stdin.readline().split()) | |
def bfs(start, end, up, down, F): | |
visited = set() | |
queue = deque() | |
queue.append((0, start)) | |
visited.add(start) | |
while queue: | |
cnt, stairs = queue.popleft() | |
if stairs == end: | |
return cnt | |
if stairs + up <= F and stairs + up not in visited: | |
visited.add(stairs+up) | |
queue.append((cnt+1, stairs+up)) | |
if stairs - down >= 1 and stairs - down not in visited: | |
visited.add(stairs-down) | |
queue.append((cnt+1, stairs-down)) | |
return "use the stairs" | |
print(bfs(S, G, U, D, F)) |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 백준 12015. 가장 긴 증가하는 부분수열 2(LIS) (0) | 2020.07.14 |
---|---|
[Python] 백준 2644. 촌수계산 (0) | 2020.07.13 |
[Python] 백준 1244. 스위치 켜고 끄기 (0) | 2020.07.08 |
[Python] 백준 19236. 청소년 상어 (0) | 2020.07.06 |
[Python] 백준 17144. 미세먼지 안녕! (0) | 2020.05.20 |