본문 바로가기
Algorithm/BFS, DFS

[백준] 1967번 숨바꼭질

by 등촌동 꼬북이 2020. 9. 15.

처음 코드는 너무 성능이 느렸는데 개선해서

 

굉장히 빨라졌다..

 

약간 for문을 다양하게 쓰는 방법을 익힌거 같아서 기분이 좋고

 

좀 더 개선이 가능할 것으로 생각되서

 

개선은 내일.. 

 

# 1697
from collections import deque

LIMIT = 100001

N, K = map(int, input().split())

def find():
    counter = 0
    que = deque([N])
    qc = deque([0])

    while que:
        temp = que.popleft()
        counter = qc.popleft()

        if temp == K:
            break

        for nx in (temp+1, temp-1, temp*2):
            if 0 <= nx < LIMIT and not mapData[nx]:
                que.append(nx)
                qc.append(counter + 1)
                mapData[nx] = 1
    return counter

mapData = [0] * LIMIT
print(find())

'Algorithm > BFS, DFS' 카테고리의 다른 글

[백준] 11724번 연결 요소의 개수  (0) 2020.09.20
[백준] 16953번 A → B  (0) 2020.09.20
[백준] 4963번 섬의 개수  (0) 2020.09.14
[백준] 7576번 토마토  (0) 2020.09.14
[ 백준] 2606번 바이러스  (0) 2020.09.09

댓글