쉬운 문제
from collections import deque
LIMIT = 100001
visted = [0] * LIMIT
A, B, N, M = map(int, input().split())
que = deque()
que.append([N, 0])
ans = 0
while que:
cx, cc = que.popleft()
if cx == M:
ans = cc
break
if visted[cx] == 1:
continue
visted[cx] = 1
for val in [cx + 1, cx - 1, cx + A, cx + B, cx - A, cx - B, cx * A, cx * B, cx - (cx * A), cx - (cx * B)]:
if -1 < val < LIMIT and not visted[val]:
que.append([val, cc + 1])
print(ans)
'Algorithm > BFS, DFS' 카테고리의 다른 글
[백준] 11725번 트리의 부모찾기 (0) | 2021.02.08 |
---|---|
[백준] 18352번 특정 거리의 도시 찾기 (0) | 2021.02.08 |
[백준] 12851번 숨바꼭질 2 (0) | 2020.12.04 |
[백준] 2206번 벽 부수고 이동하기 (0) | 2020.09.24 |
[백준] 7562번 나이트의 이동 (0) | 2020.09.23 |
댓글