링크 : https://www.acmicpc.net/problem/5014
5014번: 스타트링크
첫째 줄에 F, S, G, U, D가 주어진다. (1 ≤ S, G ≤ F ≤ 1000000, 0 ≤ U, D ≤ 1000000) 건물은 1층부터 시작하고, 가장 높은 층은 F층이다.
www.acmicpc.net
- 문제
- 소요시간: 17분 1초
- 설계하기(접근방법)
1. 입력받기
건물 크기, 목표 층, 시작 층
위 버튼 아래 버튼을 입력받는다
2. 구현하기
dx = [up,down]으로 선언해서
bfs를 돈다
3. 출력하기
목표 층에 방문한 경우 최단경로 횟수를
방문하지 않았다면 use the stairs를 출력한다
- 코드(출력)
from collections import deque
building, start, end, up, down = map(int, input().split())
visited = [0 for _ in range(building + 1)]
visited[0] = -1
def bfs(building, start, end, up, down):
q = deque()
dx = [up, -down]
q.append(start)
while q:
x = q.popleft()
for i in range(2):
nx = x + dx[i]
if 1 <= nx < building + 1 and visited[nx] == 0:
visited[nx] = visited[x] + 1
q.append(nx)
visited[start] = 1
bfs(building, start, end, up, down)
if visited[end] == 0:
print('use the stairs')
else:
print(visited[end] -1 )
- 얻어갈 부분
1. append를 실수해서 디버깅이 3~4분 걸렸고, 처음층을 visited처리를 안해서 디버깅이 더 걸렸다. 주의하자
'알고리즘(백준) > BFS & DFS' 카테고리의 다른 글
[알고리즘/BFS & DFS] 7576번 : 토마토 - python (1) | 2024.02.03 |
---|---|
[알고리즘/BFS & DFS] 6593번 : 상범 빌딩 - python (0) | 2024.02.03 |
[알고리즘/BFS & DFS] 1743번 : 음식물 피하기 - python (1) | 2024.02.03 |
[알고리즘/BFS & DFS] 13913번 : 숨바꼭질 4 - python (0) | 2024.02.03 |
[알고리즘/BFS & DFS] 13549번 : 숨바꼭질 3 - python (0) | 2024.02.03 |