1from collections import deque
 2
 3def solution(x, y, n):
 4    q = deque()
 5    visited = [False] * 1000001
 6    q.append((x, 0))
 7    
 8    while q:
 9        cx, cnt = q.popleft()
10        if cx == y:
11            return cnt
12        
13        if cx + n <= y and not visited[cx+n]:
14            q.append((cx+n, cnt+1))
15            visited[cx+n] = True
16        if cx * 2 <= y and not visited[cx*2]:
17            q.append((cx*2, cnt+1))
18            visited[cx*2] = True
19        if cx * 3 <= y and not visited[cx*3]:
20            q.append((cx*3, cnt+1))
21            visited[cx*3] = True
22    return -1
문제
- 자연수 x를 y로 변환시킬려고 한다
- 할 수 있는 연산은 3가지 (x에 n을 곱한다, x에 2를 곱한다, x에 3을 곱한다)
- 필요한 최소 연산 횟수를 구하여라
- TC
- input
10(x), 40(y), 5(n) 
- ouput
2 
 
- input
해결방법
- BFS를 수행한다, 이때 방문한 숫자는 다시 방문하지 않는다