ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [bfs] 1697_숨바꼭질
    Backjoon/bfs 2022. 9. 25. 00:58

    code

    from collections import deque
    
    n,k =map(int,input().split())
    m = [0 for i in range(100001)]
    q = deque()
    q.append(n)
    direction = [-1,1,2]
    
    def bfs():
        while q:
            cur = q.popleft()
            if cur == k:
                print(m[k])
                return
    
            for i in range(3):
                if i == 2:
                    nn = cur * direction[i]
                else:
                    nn = cur + direction[i]
                if 0<=nn<=100000:
                    if not m[nn]:
                        m[nn] = m[cur] + 1
                        q.append(nn)
    
    bfs()

            if cur == k:
                print(m[k])
                return

    부분을 상단으로 올리니 런타임 오류 해결

    왜,,?

    'Backjoon > bfs' 카테고리의 다른 글

    [bfs] 2178_미로 탐색  (0) 2022.09.21
    [bfs] 1926_그림  (0) 2022.09.21
Designed by Tistory.