-
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
10-H0ngJu #177
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.
์์ด๊ณ ... ํ์ฃผ๋ ์ฝ๋๋ฅผ ๋ณด๊ณ ์ ์ฝ๋๋ฅผ ๋ณด๋ ๋ถ๋๋ฝ๋ค์ ^^;;
์ฒ์์ ๋ฐฑํธ๋ํน์ผ๋ก ์ ๊ทผ์ ํ๋ค๊ฐ ์๊ฐ์ด๊ณผ์ ๋ํต ๋นํ๊ณ ,
BFS๋ก ํ์๋๋ฐ ์๋ ์ฌ์ง ์ฒ๋ผ 46ํผ์์ ๊ฑธ๋ ค์..
ํ์ฃผ๋ PR ์ฐธ๊ณ ํ๊ณ parent
๋ฅผ ์ ์ฅํ๋ฉด ๋๋ค๋ ์ธ์ฌ์ดํธ๋ฅผ ์ป๊ณ ๋๋ฑํ์ต๋๋ค.
์ฌ์ด ๋ฏ ํ๋ฉด์๋ ์ญ์ถ์ ํ๋ ๊ณผ์ ์ด ๋ฐ๋ก ๋ ์ค๋ฅด์ง ์์๋ค์..
์ข์ ๋ฌธ์ ๊ฐ์ฌํฉ๋๋ค !!
from collections import deque
import sys
def input(): return sys.stdin.readline().rstrip()
N, K = map(int, input().split())
parent = [-1 for _ in range(100001)]
visited = set()
deq = deque([[N, 0]])
while deq:
now, now_count = deq.popleft()
if now == K:
print(now_count)
if N == K:
print(N)
break
answer = deque([now])
prev = parent[now]
while prev != N:
answer.appendleft(prev)
prev = parent[prev]
answer.appendleft(N)
print(*answer)
break
if now != 0 and now*2 <= 100000 and now*2 not in visited:
visited.add(now*2)
parent[now*2] = now
deq.append([now*2, now_count+1])
if now-1 >= 0 and now-1 not in visited:
visited.add(now-1)
parent[now-1] = now
deq.append([now-1, now_count+1])
if now < K and now+1 not in visited:
visited.add(now+1)
parent[now+1] = now
deq.append([now+1, now_count+1])
`
while q: | ||
size = len(q) | ||
for _ in range(size): | ||
location = q.popleft() | ||
if location == K: | ||
print(time) | ||
answer = [] | ||
while location != -1: | ||
answer.append(location) | ||
location = parent[location] | ||
answer.reverse() | ||
print(" ".join(map(str, answer))) | ||
break | ||
for next_location in (location + 1, location - 1, location * 2): | ||
if 0 <= next_location <= 100000 and not visited[next_location]: | ||
q.append(next_location) | ||
visited[next_location] = 1 | ||
parent[next_location] = location | ||
time += 1 |
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.
์ค... ์ ๋ time์ ๋ปํ๋ count๋ฅผ ํ ๋ด๋ถ์ ๋ฃ์ด์ ์ฐ์ฐํ๋๋ฐ,
์ด๋ฐ ์์ผ๋ก ํ๋ฉด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํ ์๋ ์๊ฒ ๋ค์... ์ ๋ฐํด์
for next_location in (location + 1, location - 1, location * 2): | ||
if 0 <= next_location <= 100000 and not visited[next_location]: | ||
q.append(next_location) | ||
visited[next_location] = 1 | ||
parent[next_location] = location |
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.
ํ ์ด๋ ๊ฒ ์ข์ ๋ฐฉ๋ฒ์ด.... ๋ฐฐ์ฐ๊ณ ๊ฐ๋๋ค..
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.
์ ๋ ๋ถ๋ชจ ์ถ๋ ฅํ๋ ์ญ์ถ์ ๋ก์ง์ ์ฌ๊ท๋ก ์ง๊ณค ํฉ๋๋ค. ์ ์ง ์ด๊ฒ ๋ ์ต์ํ๋๋ผ๊ตฌ์ ใ
ใ
distances ๋ฐฐ์ด์ ์ด๊ธฐ๊ฐ์ 100001(์กฐ๊ฑด ์ ๋์ฌ ์ ์๋ ์ต๋ ๊ฑฐ๋ฆฌ๊ฐ)์ผ๋ก ์ค์ ํด๋๊ณ , next ๊ฑฐ๋ฆฌ๊ฐ์ด current ๊ฑฐ๋ฆฌ๊ฐ + 1๋ณด๋ค ์์ ๊ฒฝ์ฐ์๋ง queue์ ๋ค์ด๊ฐ๋๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ณ๋์ ๋ณ์๋ฅผ ์์จ๋ ๋๋ต๋๋ค :)
from collections import deque
from sys import stdin, setrecursionlimit
setrecursionlimit(10**6)
input = stdin.readline
n, k = map(int, input().split())
distances = [100001] * 100001
parents = [-1] * 100001
q = deque([n])
distances[n] = 0
def out_of_bound(x: int) -> bool:
return not (0 <= x <= 100000)
while q:
x = q.popleft()
if x == k:
print(distances[x])
break
for nx in (x * 2, x - 1, x + 1):
if out_of_bound(nx) or distances[nx] < distances[x] + 1:
continue
distances[nx] = distances[x] + 1
parents[nx] = x
q.append(nx)
def dfs(x: int = k):
if(x == -1):
return
dfs(parents[x])
print(x, end=' ')
dfs()
์๋๋ฉด ๋ถ๋ชจ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ ๋, deque์ ์จ์ front์ ๊ณ์ ๋ฐ์ด๋ฃ๋ ์์ผ๋ก ํ๋ฉด ๊ตณ์ด reverse ํ ํ์ ์๊ฒ ์ฃ ?
paths = deque([k])
while parents[k] != -1:
k = parents[k]
paths.appendleft(k)
print(' '.join(map(str, paths)))
๐ ๋ฌธ์ ๋งํฌ
์จ๋ฐ๊ผญ์ง 4
โ๏ธ ์์๋ ์๊ฐ
(์จ๋ฐ๊ผญ์ง ํฌํจ) 2์๊ฐ
โจ ์๋ ์ฝ๋
์ด๋ฒ ๋ฌธ์ ๋ ์จ๋ฐ๊ผญ์ง๋ฅผ ๋จผ์ ํ๊ณ ํ์์ต๋๋ค. ์จ๋ฐ๊ผญ์ง 4๊ฐ ๊ฝค ์ค๋๊ฑธ๋ ธ์ต๋๋ค.
๋จผ์ ์์น N์์ ์๋น์ด๊ฐ ๊ฐ ์ ์๋
next_location
์[location+1, location-1, location*2] ์ค ํ๋์ ๋๋ค.
๋ฐ๋ผ์ ๋ค์์ ์์น๋ค์ ํ์ ๋ฃ๊ณ ๋ฐฉ๋ฌธ๋ ธ๋์์ ํ์ํด์ฃผ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋ ๊ฒ location์ popํ๊ณ next_location์ appendํ๋ฉด์ ๋์์์น(K)์ ๋๋ฌํ ๋๊น์ง ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด ๋ฉ๋๋ค.
์จ๋ฐ๊ผญ์ง 4๋ ๊ธฐ์กด ์จ๋ฐ๊ผญ์ง์์ ์ถ๊ฐ๋๋ ์กฐ๊ฑด์ ๋ฐ๋ก ์๊ณ , ์ถ๋ ฅ์ผ๋ก ์๋น์ด๊ฐ ๋์๊น์ง ๊ฐ๋ ๊ฒฝ๋ก๋ฅผ ์ถ๋ ฅํ๋ฉด ๋ฉ๋๋ค.
์จ๋ฐ๊ผญ์ง ์ฝ๋์์ q๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด,
์ ๊ฐ์ด ๋์ค๊ธฐ ๋๋ฌธ์, ๊ฐ๊ฐ์ ์์๊ฐ ์ด๋ค ๋ถ๋ชจ๋ ธ๋์์ ๋์๋์ง ํ๋จ์ด ๋ถ๊ฐํฉ๋๋ค.
๋ง์ฝ, ๋ถ๋ชจ๋ ธ๋(์ด์ ์์น)๊ฐ ์ด๋์ง๋ง ์๋ฉด ๊ฑฐ์ฌ๋ฌ๊ฐ๋ฉด ๋๊ธฐ ๋๋ฌธ์,
ํ์ฌ์์น
location
- ๋ค์์์นnext_location
์ ์์ผ๋ก ์ ์ฅํ๋ฉด ํด๊ฒฐ๋ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์์ต๋๋ค.๋ฌธ์ ๋ ์ด๋ค ์๋ฃ๊ตฌ์กฐ๋ก ์ ์ฅํ ๊ฒ์ธ๊ฐ? ์๋๋ฐ
์ฒ์์๋ '๋ฑ์ ํํ ํ์์ผ๋ก (
location
,next_location
)์ ์ฅํ ๊น?' ํ๊ณ ์๊ฐํ์์ต๋๋ค.๊ทธ๋ฐ๋ฐ ์ด์ฐจํผ ๋ฐ๋ณต๋ฌธ ์์์ popํ๊ณ ์๋ก appendํ ๊ฑฐ๋ผ๋ฉด (
location
,nex_location
)์ด ๊ณ์ ๋จ์์๋๊ฒ ์๋๊ธฐ ๋๋ฌธ์, ๊ฑฐ์ฌ๋ฌ ๊ฐ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํ์์ต๋๋ค.๊ฐ๊ฐ์ ํ์ฌ์์น์ ๋ํ์ฌ next_location์ ์ ์ฅํ๋ ค๊ณ ํ๋ ๊น๋ญ์,
next_location = 6์ด๋ผ๋ฉด, location์ด 5,7,3์ผ ๋์ฒ๋ผ,
_ํ ์์น์ ๋ํด ๊ฐ ์ ์๋ ๊ฒฝ๋ก๊ฐ ์ฌ๋ฌ ๊ฐ๋ผ๊ณ ํ๋จ_ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.
(
์ง๊ธ ์๊ฐํด๋ณด๋ฉด ์์ผ๋ก ์ ์ฅํ๋ค๊ณ ํ๋๋ผ๋ ๊ฒฝ๋ก๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ์ ๋ํด์ ํด๊ฒฐ์ ํ ์ ์๋ค์)๊ทธ๋ฐ๋ฐ BFS ํน์ฑ์, ๊ฐ์ฅ ๋จผ์ ๋๋ฌํ๋ ๊ฒฝ๋ก๊ฐ ์ต๋จ๊ฒฝ๋ก์ด๋ฏ๋ก, ํ ์์น์ ๋ํด์ ๊ฒฝ๋ก๊ฐ ์ฌ๋ฌ ๊ฐ๋ผ๋ ์ ์ ์๊ฐํ์ง ์์๋ ๋ฉ๋๋ค.
์ฆ, ๋ชฉํ๊ฐ์ ์ฐพ๋ ์๊ฐ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ์์ ์๊ฐํ๋ ์ ๋ค์ ๋ฌด์ํ๊ณ , '๋ถ๋ชจ๋ ธ๋-์์๋ ธ๋'๋ง์ ์ฐ๊ฒฐ๋ก๋ง์ผ๋ก๋ ์ถฉ๋ถํ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ ์ ์๋ค๋ ์๋ฏธ์ ๋๋ค.
๋ฐฐ์ด์ ๊ทธ๋ฅ ๋ค์ ์์น๋ฅผ index๊ฐ์ผ๋ก ํ์ฌ ์์น๋ฅผ ์ ์ฅํ๋ฉด ๊ฑฐ์ฌ๋ฌ ๊ฐ ์ ์๊ฒ ๋ค ์ถ์ด์ ์๋์ ๊ฐ์ด ๊ตฌํํ์์ต๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ