Skip to content

Commit bd24e68

Browse files
committed
[BOJ]#5014. 스타트링크/실버1/23min
https://www.acmicpc.net/problem/5014
1 parent 79092b9 commit bd24e68

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Hongjoo/백준/스타트링크.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
[BOJ] 스타트링크/ 실버1
3+
4+
- 총 f 층, 현위치 s 층 , 목적지 G층
5+
- 이동 방법 (1) 위로 U 층 , (2) 아래로 D 층
6+
- 출력 : 최소 버튼 횟수 (불가능하면 "use the staris")
7+
- 1 <= s , g <= f <= 10^6
8+
#FLOW : 최단 거리 = BFS
9+
1. 총 0 ~f 층 방문 배열 생성(미방문 조건 -> 최단 거리 확보)
10+
2. BFS 진행
11+
- qeueu : [현 위치하는 층수 ] , 방문 여부 visited[층수] = 버튼 횟수(: -1)
12+
- 탐색 범위 : 1 <= nn <= f
13+
"""
14+
import sys
15+
from collections import deque
16+
input = sys.stdin.readline
17+
18+
19+
total_floors , cp , tp , up , down = map(int, input().split())
20+
21+
22+
def bfs(start, end , total_floors, up , down):
23+
building = [-1 for _ in range(total_floors+1)]
24+
#1. 시작 위치 start 의 초기화
25+
q = deque([start])
26+
building[start] = 0
27+
#2. bfs 탐색
28+
while q :
29+
cn = q.popleft()
30+
cbutton = building[cn]
31+
# pruning 조건 :
32+
if cn == end :
33+
break
34+
for dh in [up , -down] : # 엘베 2가지 조작 방법 :up , down
35+
nn = cn + dh
36+
# 다음 층이 건물 층수 범위내에 존재함& 미방문=> 방문하기
37+
if 1 <= nn <= total_floors and building[nn] <0 :
38+
q.append(nn)
39+
building[nn] = cbutton +1
40+
return building[end]
41+
42+
43+
answer=bfs(cp , tp , total_floors , up , down)
44+
#3. 출력 형식 지정
45+
if answer < 0 :
46+
print("use the stairs")
47+
else :
48+
print(answer)

0 commit comments

Comments
 (0)