대놓고 bfs
import sys
input = sys.stdin.readline
from collections import deque
def bfs(start):
visited[start] = True
q = deque()
q.append([start,''])
while q:
cur_register, commands = q.popleft()
if cur_register==target:
return commands
for command in 'DSLR':
if command=='D':
next_register = (cur_register*2)%10000
elif command=='S':
next_register = (cur_register-1)%10000
elif command=='L':
next_register = (cur_register%1000)*10 + cur_register//1000
else:
next_register = cur_register//10 + (cur_register%10)*1000
if not visited[next_register]:
visited[next_register] = True
q.append([next_register,commands+command])
for _ in range(int(input())):
start, target = map(int, input().split())
visited = {i: False for i in range(10000)}
print(bfs(start))
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 9461 파도반 수열 (0) | 2021.09.07 |
---|---|
[백준][Python] 9375 패션왕 신해빈 (0) | 2021.09.07 |
[백준][Python] 7569 토마토 (0) | 2021.09.07 |
[백준][Python] 5525 IOIOI (0) | 2021.09.07 |
[백준][Python] 1949 우수마을 (0) | 2021.09.07 |