-
Notifications
You must be signed in to change notification settings - Fork 2
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
33-alstjr7437 #236
Conversation
There was a problem hiding this 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๋ผ๊ณ ๋ฌธ์ ์ ์จ์ ธ์๋ ๋๋์ด๋ผ ๋ฐ๋ก ์ฝ๋ฉํธ๋ ์๋ฌ๊ฒ ์ต๋๋ค!!
๊ทผ๋ฐ ๊ทธ๋์ ๋ ์ฝ๋ ๊ฐ๋ ์ฑ์ด ๊ต์ฅํ ์ข๋ค์
There was a problem hiding this 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"))
๐ ๋ฌธ์ ๋งํฌ
DSLR
์~ ์์ผ์ธ์~~!
โ๏ธ ์์๋ ์๊ฐ
15๋ถ + ์๊ฐ์ด๊ณผ ๋ฌธ์ ๋ก 10๋ถ ใทใท..
โจ ์๋ ์ฝ๋
ํ์์ BFSํ๋ฉด ์ขํ๋ฅผ ๊ฑด๋๋ฆฌ๋ ๋ฌธ์ ์๋๋ฐ ์ด๋ฒ ๋ฌธ์ ๋ ๋น์ทํ์ง๋ง DSLR ๊ฐ ์ปค๋งจ๋์ ๋ฐ๋ผ ์ซ์๋ฅผ ๋ณ๊ฒฝํ๋ฉด ๋๊ฒ ๋๋ผ๊ตฌ์!
๋ฑ ์ซ์๋ 10000๊น์ง๋๊น ๊ทธ๋ฅ Visited๋ฐฐ์ด๋ก ๋ง๋ค์ด์ ์ฌ์ฉํ์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ด๊ฒ ์ฌ๋์ ์ ์์ ๋๋ฌผ์ด๋ผ๊ณ ๊ณ์ํด์ ํจ์ ์ฐ๋๋ฐ
Swfit๋ฅผ ์์ฆ ๋~~๋ฌด ๋ง์ด์จ์
func๋ก ํด๋ฒ๋ฆฌ๊ณ : ๋ฅผ ๋ฃ์ด์ผํ๋๋ฐ {}๋ฅผ ๋ฃ๊ณ ์ปจํ ์คํธ ์ค์์นญ์ด ์์๋๋ค์...
๊ทธ๋ฆฌ๊ณ Python์ค๋๋ง์ด๋ผ ์๊ฐ์ด๊ณผ ์๊ฐ์ ์ํด์ ์ ์๊ฐ์ด๊ณผ ๋์ง ๊ณ์ ์ฝ๋ ๋ง์ ธ๋ณด๋ค๊ฐ...
Pypy๋ก ๋ฐ๊พธ๋๊น ๋์ต๋๋ค!!