๋ฐฑ์ค€ 9019 DSLR

passยท2022๋…„ 11์›” 14์ผ
0

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ชฉ๋ก ๋ณด๊ธฐ
20/35

๋ฌธ์ œ

๐Ÿ‘€ ๋ฌธ์ œ ์‚ฌ์ดํŠธ : https://www.acmicpc.net/problem/9019






ํ’€์ด

๋‚œ์ด๋„ : Gold IV

์ด ๋ฌธ์ œ๋Š” D, S, L, R ์ด๋ผ๋Š” ๋„ค ๊ฐ€์ง€ ์—ฐ์‚ฐ์ด ์žˆ๋Š”๋ฐ, ์ž…๋ ฅ๋ฐ›์€ ๋‘ ์ˆซ์ž a, b์— ๋Œ€ํ•˜์—ฌ ์—ฐ์‚ฐ๋“ค์„ ์ž˜ ์กฐํ•ฉํ•ด์„œ ์ตœ์†Œํ•œ์˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ a๋ฅผ b๋กœ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

โœ” ์ด ๋ฌธ์ œ์—์„œ ์ƒ๊ฐํ•ด๋ด์•ผ ํ•  ์ ์€ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๊ณผ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์ด๋‹ค.

์šฐ์„  ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ bfs๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

1) ์ฒ˜์Œ์— a๋ผ๋Š” ์ˆซ์ž๋ฅผ queue์— ๋„ฃ์–ด๋‘๊ณ , ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œ (visited = True)
2) ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ queue๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ bfs๋ฅผ ์ง„ํ–‰
3) queue์—์„œ ๊ฐ’์„ popํ•˜์—ฌ ๊ทธ ์ˆซ์ž์— ๋Œ€ํ•œ D, S, L, R ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ฐ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด visited ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ํ›„์—, q์— ๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.
4) 3)์—์„œ queue pop์„ ํ•  ๋•Œ๋งˆ๋‹ค b์™€ ๊ฐ™์€ ์ˆซ์ž์ธ์ง€ ๋น„๊ต ํ›„ ๊ฐ™์„ ๊ฒฝ์šฐ ๊ฒฐ๊ณผ ์ถœ๋ ฅ ํ›„ break



โœ” ์ฃผ์˜ํ•  ์ 

  • ์—ฐ์‚ฐ์ž L๊ณผ R์€ ์ž๋ฆฟ์ˆ˜ ์ด๋™ ์—ฐ์‚ฐ์ธ๋ฐ, 2์ง„์ˆ˜ ๋น„ํŠธ์—ฐ์‚ฐ์ž๋ฅผ ์‹ญ์ง„์ˆ˜์— ํ•œ๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

  • ๋˜ํ•œ ๋„ค ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋”๋ผ๋„ ๋นˆ ์ž๋ฆฌ ์ˆ˜๋Š” 0์œผ๋กœ ์ฑ„์›Œ์ ธ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

  • ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์˜คํ•ดํ•˜๋ฉด ์•ˆ๋˜๋Š” ๋ถ€๋ถ„ ์˜ˆ์‹œ
    L :
    123 -> 312 x
    123 -> 3012 o

    R :
    123 -> 231 x
    123 -> 1230 o



โœ” ์ฐธ๊ณ 

  • python์—์„œ ์ด ๋ฌธ์ œ์™€ ๊ฐ™์ด list๋กœ ๋˜์–ด ์žˆ๋Š” ๊ฒฐ๊ณผ๋ฅผ string์œผ๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ์ถœ๋ ฅํ•  ๊ฒฝ์šฐ์— .join()์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • python์œผ๋กœ ์ด ๋ฌธ์ œ๋ฅผ ๋ฐฑ์ค€์— ์ œ์ถœํ•  ๊ฒฝ์šฐ python3๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ, pypy3๋กœ ์ œ์ถœํ•ด์•ผ ํ•œ๋‹ค.






์ฝ”๋“œ

from collections import deque

def getDSLR(x):
    dslr = []
    
    # DSLR
    dslr.append((x * 2) % 10000)
    dslr.append(x - 1 if x != 0 else 9999)
    dslr.append((x * 10) % 10000 + (x * 10) // 10000)
    dslr.append((x % 10) * 10000 // 10 + (x // 10))

    return dslr

def main():
    t = int(input())

    # DSLR ๋ฌธ์ž
    dslr_str = ["D", "S", "L", "R"]

    for _ in range(t):
        a, b = map(int, input().split())

        # bfs ์‚ฌ์šฉ : visited, deque ์„ค์ •
        visited = [False] * (10001)
        q = deque()
        visited[a] = True
        q.append((a, []))

        while q:
            a, results = q.popleft()
            
            if a == b:
                print(''.join(results))
                break
            
            dslr = getDSLR(a)

            for i in range(len(dslr)):
                if not visited[dslr[i]]:
                    visited[dslr[i]] = True
                    q.append((dslr[i], results + [dslr_str[i]]))

if __name__ == "__main__":
    main()
profile
์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€