[알고리즘 문제풀이] 숨바꼭질

황인권·2023년 4월 7일
0

알고리즘 문제풀이

목록 보기
38/81

문제 제목 : 숨바꼭질

문제 난이도 : 하

문제 유형 : BFS, 그래프

https://www.acmicpc.net/problem/1697
시간 제한 : 2초
메모리 제한 : 128MB

문제풀이 아이디어

< 소스코드 >

from collections import deque

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

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

print(bfs())
profile
inkwon Hwang

0개의 댓글