티스토리 뷰

"""
 *packageName    : 
 * fileName       : 1697_숨바꼭질_S1
 * author         : ipeac
 * date           : 2022-04-19
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-04-19        ipeac       최초 생성
 """
from collections import deque

n, k = map(int, input().split())
graph = [0 for _ in range(10 ** 6)]


def bfs(x):
      queue = deque([x])
      
      while queue:
            x = queue.popleft()
            if x == k:
                  return graph[x]
            
            for i in (x - 1, x + 1, 2 * x):
                  
                  if 0 <= i <= 100000 and graph[i] == 0:
                        graph[i] = graph[x] + 1
                        queue.append(i)


print(bfs(n))

-BFS 풀이

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함