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

10-H0ngJu #177

Merged
merged 2 commits into from
May 3, 2024
Merged

10-H0ngJu #177

merged 2 commits into from
May 3, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Apr 3, 2024

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

์ˆจ๋ฐ”๊ผญ์งˆ 4

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

(์ˆจ๋ฐ”๊ผญ์งˆ ํฌํ•จ) 2์‹œ๊ฐ„

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

์ด๋ฒˆ ๋ฌธ์ œ๋„ ์ˆจ๋ฐ”๊ผญ์งˆ๋ฅผ ๋จผ์ € ํ’€๊ณ  ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ˆจ๋ฐ”๊ผญ์งˆ 4๊ฐ€ ๊ฝค ์˜ค๋ž˜๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ์œ„์น˜ N์—์„œ ์ˆ˜๋นˆ์ด๊ฐ€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” next_location์€
[location+1, location-1, location*2] ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ์˜ ์œ„์น˜๋“ค์„ ํ์— ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ๋…ธ๋“œ์ž„์„ ํ‘œ์‹œํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ฒŒ location์„ popํ•˜๊ณ  next_location์„ appendํ•˜๋ฉด์„œ ๋™์ƒ์œ„์น˜(K)์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

N, K = map(int, input().split())

visited = [0] * 100001
time = 0

q = deque([N])
visited[N] = 1

while q:
    size = len(q)
    for _ in range(size):
        location = q.popleft()
        if location == K:
            print(time)
            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
    time += 1

์ˆจ๋ฐ”๊ผญ์งˆ 4๋Š” ๊ธฐ์กด ์ˆจ๋ฐ”๊ผญ์งˆ์—์„œ ์ถ”๊ฐ€๋˜๋Š” ์กฐ๊ฑด์€ ๋”ฐ๋กœ ์—†๊ณ , ์ถœ๋ ฅ์œผ๋กœ ์ˆ˜๋นˆ์ด๊ฐ€ ๋™์ƒ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ˆจ๋ฐ”๊ผญ์งˆ ์ฝ”๋“œ์—์„œ q๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด,
image
์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ ์–ด๋–ค ๋ถ€๋ชจ๋…ธ๋“œ์—์„œ ๋‚˜์™”๋Š”์ง€ ํŒ๋‹จ์ด ๋ถˆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.


๋งŒ์•ฝ, ๋ถ€๋ชจ๋…ธ๋“œ(์ด์ „ ์œ„์น˜)๊ฐ€ ์–ด๋”˜์ง€๋งŒ ์•Œ๋ฉด ๊ฑฐ์Šฌ๋Ÿฌ๊ฐ€๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์—,
ํ˜„์žฌ์œ„์น˜ location - ๋‹ค์Œ์œ„์น˜ next_location์„ ์Œ์œผ๋กœ ์ €์žฅํ•˜๋ฉด ํ•ด๊ฒฐ๋  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.


๋ฌธ์ œ๋Š” ์–ด๋–ค ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ์ €์žฅํ•  ๊ฒƒ์ธ๊ฐ€? ์˜€๋Š”๋ฐ

์ฒ˜์Œ์—๋Š” '๋ฑ์— ํŠœํ”Œ ํ˜•์‹์œผ๋กœ (location, next_location)์ €์žฅํ• ๊นŒ?' ํ•˜๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ์–ด์ฐจํ”ผ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ popํ•˜๊ณ  ์ƒˆ๋กœ appendํ•  ๊ฑฐ๋ผ๋ฉด (location, nex_location)์ด ๊ณ„์† ๋‚จ์•„์žˆ๋Š”๊ฒŒ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๊ฑฐ์Šฌ๋Ÿฌ ๊ฐ€๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.


๊ฐ๊ฐ์˜ ํ˜„์žฌ์œ„์น˜์— ๋Œ€ํ•˜์—ฌ next_location์„ ์ €์žฅํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๊นŒ๋‹ญ์€,
next_location = 6์ด๋ผ๋ฉด, location์ด 5,7,3์ผ ๋•Œ์ฒ˜๋Ÿผ,
_ํ•œ ์œ„์น˜์— ๋Œ€ํ•ด ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๊ณ  ํŒ๋‹จ_ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
(์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์Œ์œผ๋กœ ์ €์žฅํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ฒฝ๋กœ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ํ•ด๊ฒฐ์„ ํ•  ์ˆ˜ ์—†๋„ค์š”)


๊ทธ๋Ÿฐ๋ฐ BFS ํŠน์„ฑ์ƒ, ๊ฐ€์žฅ ๋จผ์ € ๋„๋‹ฌํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์ตœ๋‹จ๊ฒฝ๋กœ์ด๋ฏ€๋กœ, ํ•œ ์œ„์น˜์— ๋Œ€ํ•ด์„œ ๊ฒฝ๋กœ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋Š” ์ ์„ ์ƒ๊ฐํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, ๋ชฉํ‘œ๊ฐ’์„ ์ฐพ๋Š” ์ˆœ๊ฐ„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ž์„œ ์ƒ๊ฐํ–ˆ๋˜ ์ ๋“ค์„ ๋ฌด์‹œํ•˜๊ณ , '๋ถ€๋ชจ๋…ธ๋“œ-์ž์‹๋…ธ๋“œ'๋งŒ์˜ ์—ฐ๊ฒฐ๋กœ๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

๋ฐฐ์—ด์— ๊ทธ๋ƒฅ ๋‹ค์Œ ์œ„์น˜๋ฅผ index๊ฐ’์œผ๋กœ ํ˜„์žฌ ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๋ฉด ๊ฑฐ์Šฌ๋Ÿฌ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒ ๋‹ค ์‹ถ์–ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import sys
from collections import deque

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

N, K = map(int, input().split())

visited = [0] * 100001
time = 0
parent = [-1] * 100001

q = deque([N])
visited[N] = 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

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

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.

์•„์ด๊ณ ... ํ™์ฃผ๋‹˜ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋ถ€๋„๋Ÿฝ๋„ค์š” ^^;;

์ฒ˜์Œ์— ๋ฐฑํŠธ๋ž˜ํ‚น์œผ๋กœ ์ ‘๊ทผ์„ ํ–ˆ๋‹ค๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ์— ๋œํ†ต ๋‹นํ•˜๊ณ ,

BFS๋กœ ํ‹€์—ˆ๋Š”๋ฐ ์•„๋ž˜ ์‚ฌ์ง„ ์ฒ˜๋Ÿผ 46ํผ์—์„œ ๊ฑธ๋ ค์„œ..

image







ํ™์ฃผ๋‹˜ 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])

`

Comment on lines +15 to +33
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
Copy link
Member

Choose a reason for hiding this comment

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

์˜ค... ์ €๋Š” time์„ ๋œปํ•˜๋Š” count๋ฅผ ํ ๋‚ด๋ถ€์— ๋„ฃ์–ด์„œ ์—ฐ์‚ฐํ–ˆ๋Š”๋ฐ,

์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”... ์‹ ๋ฐ•ํ•ด์š”

Comment on lines +28 to +32
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
Copy link
Member

Choose a reason for hiding this comment

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

ํ—‰ ์ด๋ ‡๊ฒŒ ์ข‹์€ ๋ฐฉ๋ฒ•์ด.... ๋ฐฐ์šฐ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค..

@tgyuuAn tgyuuAn mentioned this pull request Apr 8, 2024
Copy link

@9kyo-hwang 9kyo-hwang left a 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)))

@MunbinLee MunbinLee merged commit ed75d21 into main May 3, 2024
@MunbinLee MunbinLee deleted the 10-H0ngJu branch May 3, 2024 15:39
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.

4 participants