728x90
https://www.acmicpc.net/problem/12851
12851번: 숨바꼭질 2
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때
www.acmicpc.net
아이디어
체크할 필요가 없으니 체크리스트를 제거한 뒤에 거리가 같을 경우 cnt 값을 증가시키며 답을 출력
from collections import deque
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
ch = [0] * 100001
dis = [0] * 100001
cnt = 0
tmp = 0
ch[n] = 1
dq = deque([n])
answer = -1
while dq:
now = dq.popleft()
if now == m:
cnt+=1
answer = dis[m]
continue
for next in (now+1, now-1, now*2):
if 0<=next<100001 and (dis[next] == 0 or dis[next] == dis[now]+1):
if ch[next] == 0:
dis[next] = dis[now] + 1
dq.append(next)
print(answer)
print(cnt)
완성
728x90
'알고리즘' 카테고리의 다른 글
| [백준] 14501번: 퇴사 - python (0) | 2024.05.06 |
|---|---|
| [백준] 5639번: 이진 검색 트리 - python (0) | 2024.04.12 |
| [백준] 13549번: 숨바꼭질 3 - python (0) | 2024.04.12 |
| [백준] 12865번: 평범한 배낭 - python (0) | 2024.04.11 |
| [백준] 11660번: 구간 합 구하기 5 - python (0) | 2024.04.11 |