• Home
  • About
    • Seokmin.Lee photo

      Seokmin.Lee

      Hello, I am a master's student in the Department of Convergence Security (Samsung Advanced Security) at Korea University.After graduation, I am expected as a security developer or researcher member of Samsung SDS.

    • Learn More
    • LinkedIn
    • Github
  • Posts
    • All Tags

[til][bj1697]숨바꼭질

22 Jan 2025

Code

from collections import deque

# 방문 여부를 저장하는 배열
visited = [False] * 100001

# 다음 단계를 정의하는 배열
next_step = [-1, 1]

count = 0

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

def bfs(x):
    global count
    queue = deque([(x, 0)])
    visited[x] = True

    while queue:
        current, step_count = queue.popleft()

        if current == K:
            print(step_count)
            return

        for i in range(3):
            if i == 2:
                next_x = current * 2
            else:
                next_x = current + next_step[i]

            if next_x > 100000 or next_x < 0:
                continue

            if not visited[next_x]:
                visited[next_x] = True
                queue.append((next_x, step_count + 1))

bfs(N)



Share Tweet +1