boj 1697 숨바꼭질 (실버1)

김준오·2021년 8월 22일
0

알고리즘

목록 보기
34/91
post-thumbnail

문제

boj 1697 숨바꼭질

bfs 문제이다
감 떨어질까봐 오랜만에 풀어봤다

-1, +1, 2를 계속 넣으면서 bfs로 탐색하면 된다
테스트 케이스에 100000번으로 되는게 들어있나보다
처음에 arr 크기를 [0]
100000으로 했더니 런타임에러가 떴다

내 풀이

import sys
input = sys.stdin.readline
from collections import deque

n, k = map(int,input().split())

q = deque()
q.append(n)

arr = [0] * 100001


while(q):
  a = q.popleft()
  if a == k:
    print(arr[a])
    break

  for nx in (a+1, a-1, a*2) :
    if  0 <= nx <= 100000 and arr[nx] == 0 :
      arr[nx] = arr[a] + 1
      q.append(nx)

결과

끝!

profile
jooooon

0개의 댓글

관련 채용 정보