[๋ฐฑ์ค€-9019] DSLR / Python

๐Ÿ“š Problem Solving/Baekjoon

 

9019๋ฒˆ: DSLR

๋„ค ๊ฐœ์˜ ๋ช…๋ น์–ด D, S, L, R ์„ ์ด์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ๊ธฐ๊ฐ€ ์žˆ๋‹ค. ์ด ๊ณ„์‚ฐ๊ธฐ์—๋Š” ๋ ˆ์ง€์Šคํ„ฐ๊ฐ€ ํ•˜๋‚˜ ์žˆ๋Š”๋ฐ, ์ด ๋ ˆ์ง€์Šคํ„ฐ์—๋Š” 0 ์ด์ƒ 10,000 ๋ฏธ๋งŒ์˜ ์‹ญ์ง„์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ๋ช…๋ น์–ด๋Š” ์ด ๋ ˆ์ง€์Šคํ„ฐ์—

www.acmicpc.net

import sys
from collections import deque

input = sys.stdin.readline


def bfs():
    queue = deque()
    queue.append((a, ""))
    while queue:
        num, result = queue.popleft()
        dn = (num * 2) % 10000
        if dn == b:
            return result + "D"
        elif visited[dn] == 0:
            visited[dn] = 1
            queue.append((dn, result + "D"))
        sn = num - 1 if num != 0 else 9999
        if sn == b:
            return result + "S"
        elif visited[sn] == 0:
            visited[sn] = 1
            queue.append((sn, result + "S"))
        ln = int(num % 1000 * 10 + num / 1000)
        if ln == b:
            return result + "L"
        elif visited[ln] == 0:
            visited[ln] = 1
            queue.append((ln, result + "L"))
        rn = int(num % 10 * 1000 + num // 10)
        if rn == b:
            return result + "R"
        elif visited[rn] == 0:
            visited[rn] = 1
            queue.append((rn, result + "R"))


for _ in range(int(input())):
    a, b = map(int, input().split())
    visited = [0] * 10000
    print(bfs())

 

ํ•ด์„ค

bfs๋กœ D, S, L, R์„ ํ•˜๋‚˜์”ฉ ์ˆ˜ํ–‰ํ•˜๋ฉด ์™„๋ฃŒ!