227. 숨바꼭질

아현·2021년 7월 25일
0

Algorithm

목록 보기
237/400

백준






1. BFS

참고


from collections import deque 
MAX = 100001 

def bfs(): 
    q = deque() 
    q.append(n) 
        
    while q: 
        v = q.popleft() 
        if v == k: 
            print(time[v]) 
            return 
        for next_step in (v-1, v+1, v*2): 
            if 0 <= next_step < MAX and not time[next_step]:    
                time[next_step] = time[v] + 1 
                q.append(next_step) 



n, k = map(int, input().split()) 
time = [0] * MAX 
bfs()

profile
Studying Computer Science

0개의 댓글