-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvinay.py
More file actions
50 lines (39 loc) · 1.29 KB
/
vinay.py
File metadata and controls
50 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from collections import deque
def min_operations(x, y):
if x == y:
return 0
# Queue for BFS, initialized with the starting number x and 0 steps
queue = deque([(x, 0)])
visited = set([x])
# BFS loop
while queue:
current, steps = queue.popleft()
# Generate possible next states
# If current can be halved
if current % 2 == 0:
next_state = current // 2
if next_state == y:
return steps + 1
if next_state not in visited:
visited.add(next_state)
queue.append((next_state, steps + 1))
# If current can be divided by 3
if current % 3 == 0:
next_state = current // 3
if next_state == y:
return steps + 1
if next_state not in visited:
visited.add(next_state)
queue.append((next_state, steps + 1))
# Always consider subtracting 1
next_state = current - 1
if next_state == y:
return steps + 1
if next_state not in visited:
visited.add(next_state)
queue.append((next_state, steps + 1))
return -1
T = int(input())
for _ in range(T):
x, y = map(int, input().split())
print(min_operations(x, y))