※ 1697 숨바꼭질
https://www.acmicpc.net/problem/1697
문제 해결 TIP
문제에 주어진 대로 x - 1, x + 1, x * 2 총 3가지 방법이 있다. 동생의 점 K와 같을 때까지 반복하며, 시간은 dist 배열을 통해 1씩 추가해주며 구한다.
전체 코드
from collections import deque
N, K = map(int,input().split())
def bfs():
queue = deque()
queue.append(N)
while queue:
x = queue.popleft()
if x == K:
print(dist[x])
break
for i in (x - 1, x + 1, x * 2):
if 0 <= i <= 100000 and not dist[i]:
dist[i] = dist[x] + 1
queue.append(i)
dist = [0] * 100001
bfs()
- 비기너 문제 : Hashing https://www.acmicpc.net/problem/15829
- 미들러 문제 : 숨바꼭질 https://www.acmicpc.net/problem/1697
- 챌린저 문제 : 주사위 윷놀이 https://www.acmicpc.net/problem/17825
'Python > DFS&BFS' 카테고리의 다른 글
99클럽 코테 스터디 9일차 TIL + 백준 1707 이분 그래프 (파이썬) (0) | 2025.01.23 |
---|---|
99클럽 코테 스터디 8일차 TIL + 백준 2667 단지번호붙이기 (파이썬) (0) | 2025.01.22 |
99클럽 코테 스터디 6일차 TIL + 백준 1260 DFS와 BFS (파이썬) (0) | 2025.01.21 |
[Algorithm] 백준 1261 알고스팟 | 파이썬 BFS (0) | 2024.09.11 |
[Algorithm] 백준 2665 미로만들기 | 파이썬 BFS (0) | 2024.09.11 |
댓글