[백준/파이썬] 1697번

민정·2023년 12월 27일
0

[백준/파이썬]

목록 보기
208/245
post-thumbnail

📍백준 1697번 문제

https://www.acmicpc.net/problem/1697

코드

import sys
from collections import deque
input = sys.stdin.readline


def bfs(v):
    q = deque()
    q.append(v)
    visited[v] = 0
    while q:
        v = q.popleft()
        if v == end:
            print(visited[end])
            break
        a, b, c = v-1, v+1, v*2
        if 0 <= a < 100001 and visited[a] == -1:
            visited[a] = visited[v] + 1
            q.append(a)
        if 0 <= b < 100001 and visited[b] == -1:
            visited[b] = visited[v] + 1
            q.append(b)
        if 0 <= c < 100001 and visited[c] == -1:
            visited[c] = visited[v] + 1
            q.append(c)


start, end = map(int, input().split())
visited = [-1 for _ in range(100001)]
bfs(start)
profile
パㅔバ6ㅇr 덤벼ㄹΓ :-0

0개의 댓글