Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

33-alstjr7437 #236

Merged
merged 1 commit into from
Aug 26, 2024
Merged

33-alstjr7437 #236

merged 1 commit into from
Aug 26, 2024

Conversation

alstjr7437
Copy link
Member

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

DSLR

์ž~ ์›ƒ์œผ์„ธ์š”~~!

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

15๋ถ„ + ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋กœ 10๋ถ„ ใ„ทใ„ท..

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

ํ‰์†Œ์˜ BFSํ•˜๋ฉด ์ขŒํ‘œ๋ฅผ ๊ฑด๋“œ๋ฆฌ๋Š” ๋ฌธ์ œ ์˜€๋Š”๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ๋น„์Šทํ•˜์ง€๋งŒ DSLR ๊ฐ ์ปค๋งจ๋“œ์— ๋”ฐ๋ผ ์ˆซ์ž๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ๋˜๊ฒ ๋”๋ผ๊ตฌ์š”!

๋”ฑ ์ˆซ์ž๋„ 10000๊นŒ์ง€๋‹ˆ๊นŒ ๊ทธ๋ƒฅ Visited๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

image
  1. A์™€ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ๋จผ์ € queue์— ๋„ฃ๋Š”๋‹ค
  2. queue Pop์„ ํ•˜๋ฉด์„œ ๋ฐ˜๋ณต๋ฌธ๊ณผ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ DSLR์˜ ์ˆซ์ž๋ฅผ ๊ฐ๊ฐ ๋„ฃ๋Š”๋‹ค
  3. visited == False๋ฉด true๋กœ ๋ฐ”๊พธ๊ณ  ์ˆซ์ž์™€ ํ˜„์žฌ๊นŒ์ง€ cmd๋ฅผ ๋„ฃ๋Š”๋‹ค.
  4. ๋งŒ์•ฝ cur == B๋กœ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜์™”์œผ๋ฉด ํ˜„์žฌ๊นŒ์ง€ command๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค!
from collections import deque
import sys

input = sys.stdin.readline

T = int(input())

def command(register, cmd):
    if cmd == 0:
        return ((register * 2) % 10000, "D")
    elif cmd == 1:
        return (9999 if register == 0 else register - 1, "S")
    elif cmd == 2:
        return (register % 1000 * 10 + register // 1000, "L")
    elif cmd == 3:
        return (register // 10 + (register % 10) * 1000, "R")

for _ in range(T):
    A, B = map(int, input().split())

    visited = [False] * 10000
    visited[A] = True

    queue = deque([(A, "")])
    while queue:
        cur, result = queue.popleft()
        
        if cur == B:
            print(result)
            break
        
        for i in range(4):
            register, cmd = command(cur, i)
            if not visited[register]:
                visited[register] = True
                queue.append((register, result + cmd))

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ด๊ฒŒ ์‚ฌ๋žŒ์€ ์ ์‘์˜ ๋™๋ฌผ์ด๋ผ๊ณ  ๊ณ„์†ํ•ด์„œ ํ•จ์ˆ˜ ์“ฐ๋Š”๋ฐ
Swfit๋ฅผ ์š”์ฆ˜ ๋„ˆ~~๋ฌด ๋งŽ์ด์จ์„œ
func๋กœ ํ•ด๋ฒ„๋ฆฌ๊ณ  : ๋ฅผ ๋„ฃ์–ด์•ผํ•˜๋Š”๋ฐ {}๋ฅผ ๋„ฃ๊ณ  ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ์ด ์ž˜์•ˆ๋˜๋„ค์š”...

๊ทธ๋ฆฌ๊ณ  Python์˜ค๋ž˜๋งŒ์ด๋ผ ์‹œ๊ฐ„์ดˆ๊ณผ ์ƒ๊ฐ์„ ์•ˆํ•ด์„œ ์™œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚˜์ง€ ๊ณ„์† ์ฝ”๋“œ ๋งŒ์ ธ๋ณด๋‹ค๊ฐ€...
Pypy๋กœ ๋ฐ”๊พธ๋‹ˆ๊นŒ ๋์Šต๋‹ˆ๋‹ค!!

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from collections import deque
import sys

def input(): return sys.stdin.readline().rstrip()
for _ in range(int(input())):
    A, B = map(int, input().split())

    deq = deque()
    visited = {A,}
    deq.append((A, ""))
    while deq:
        now_value, history = deq.popleft()

        if now_value == B:
            print(history)
            break

        for query in range(4):
            if query == 0:
                new_value = int(now_value*2%10000)
            
                if new_value in visited: continue
        
                new_history = history+"D"
                
            elif query == 1:
                new_value = now_value-1 if now_value != 0 else 9999

                if new_value in visited: continue
                
                new_history = history+"S"
    
            elif query == 2:
                str_now_value = str(now_value)
                while len(str_now_value) < 4:
                    str_now_value = "0" + str_now_value
                
                new_value = int(str_now_value[1]+str_now_value[2]+str_now_value[3]+str_now_value[0])
    
                if new_value in visited: continue
                
                new_history = history+"L"
    
            else:
                str_now_value = str(now_value)
                while len(str_now_value) < 4:
                    str_now_value = "0" + str_now_value
                new_value = int(str_now_value[3]+str_now_value[0]+str_now_value[1]+str_now_value[2])

                if new_value in visited: continue

                new_history = history+"R"
        
            visited.add(new_value)
            deq.append((new_value, new_history))

๋Œ€๋†“๊ณ  BFS๋ผ๊ณ  ๋ฌธ์ œ์— ์จ์ ธ์žˆ๋Š” ๋Š๋‚Œ์ด๋ผ ๋”ฐ๋กœ ์ฝ”๋ฉ˜ํŠธ๋Š” ์•ˆ๋‹ฌ๊ฒ ์Šต๋‹ˆ๋‹ค!!

๊ทผ๋ฐ ๊ทธ๋‚˜์ €๋‚˜ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ๊ต‰์žฅํžˆ ์ข‹๋„ค์š”

Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ์‹œ๊ฐ„์ดˆ๊ณผ ๋•Œ๋ฌธ์— ์ฝ”๋“œ ์ˆ˜์ •ํ•˜๋‹ค๊ฐ€ pypy3๋กœ ํ•˜๋‹ˆ๊นŒ ํ†ต๊ณผ๋˜๋„ค์šฉ
๋ฏผ์„๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ์ฐจ๋ผ๋ฆฌ ์ฒ˜์Œ๋ถ€ํ„ฐ cmd 4๊ฐœ๋ฅผ ๋ฌถ์€ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“œ๋Š”๊ฒŒ ๋” ๊น”๋”ํ–ˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!! ๐Ÿ‘๐Ÿ‘

์ˆ˜๊ณ ํ•˜์…จ์Šด๋‹ค ~~

import sys
from collections import deque

def input() : return sys.stdin.readline().rstrip()

T = int(input())
test = [list(map(int, input().split())) for _ in range(T)]

def mod_D(n):
    return (2 * n) % 10000

def mod_S(n):
    return 9999 if n == 0 else n - 1

def mod_L(n):
    d1 = n // 1000
    tmp = n % 1000
    return (tmp * 10) + d1

def mod_R(n):
    d4 = n % 10
    tmp = n // 10
    return (d4 * 1000) + tmp

for A, B in test:
    q = deque([(A, "")])
    visited = [False] * 10000
    visited[A] = True

    while q:
        num, cmd = q.popleft()

        if num == B:
            print(cmd)
            break

        d_num = mod_D(num)
        if not visited[d_num]:
            visited[d_num] = True
            q.append((d_num, cmd + "D"))

        s_num = mod_S(num)
        if not visited[s_num]:
            visited[s_num] = True
            q.append((s_num, cmd + "S"))

        l_num = mod_L(num)
        if not visited[l_num]:
            visited[l_num] = True
            q.append((l_num, cmd + "L"))

        r_num = mod_R(num)
        if not visited[r_num]:
            visited[r_num] = True
            q.append((r_num, cmd + "R"))

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants