-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSLR.py
More file actions
38 lines (36 loc) · 1.11 KB
/
DSLR.py
File metadata and controls
38 lines (36 loc) · 1.11 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
import sys
from collections import deque
T = int(sys.stdin.readline().rstrip())
for _ in range(T):
A, B = list(sys.stdin.readline().rstrip().split())
visited_arr = [False] * 10000
queue = deque()
queue.append((A, ""))
while True:
x, path = queue.popleft()
visited_arr[int(x)] = True
if int(x) == int(B):
print(path)
break
if int(x) < int(1000):
x = "0" * (4 - len(x)) + x
D = str((int(x) * 2) % 10000)
S = 0
if int(x) == 0:
S = str(9999)
else:
S = str(int(x) - 1)
L = x[1:] + x[:1]
R = x[-1:] + x[:-1]
if not visited_arr[int(D)]:
queue.append((D, path + "D"))
visited_arr[int(D)] = True
if not visited_arr[int(S)]:
queue.append((S, path + "S"))
visited_arr[int(S)] = True
if not visited_arr[int(L)]:
queue.append((L, path + "L"))
visited_arr[int(L)] = True
if not visited_arr[int(R)]:
queue.append((R, path + "R"))
visited_arr[int(R)] = True