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

test #60

Closed
wants to merge 1 commit into from
Closed

test #60

wants to merge 1 commit into from

Conversation

Munbin-Lee
Copy link
Member

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

๋ฐฑ์ค€ - ๋‹ค์ต์ŠคํŠธ๋ผ/BFS | ์ˆจ๋ฐ”๊ผญ์งˆ 3

๋ฌธ์ œ ์„ค๋ช…

  • N(0 โ‰ค N โ‰ค 100,000)๊ณผ K(0 โ‰ค K โ‰ค 100,000)๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™ํ•˜์—ฌ K์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„์„ ๊ตฌํ•ด๋ผ.
  • ์œ„์น˜๊ฐ€ X์ผ ๋•Œ ๊ฑท๋Š”๋‹ค๋ฉด 1์ดˆ ํ›„์— X-1 ๋˜๋Š” X+1๋กœ ์ด๋™ํ•œ๋‹ค.
  • ์ˆœ๊ฐ„์ด๋™์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” 0์ดˆ ํ›„์— 2*X์˜ ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค.

์ œํ•œ ์กฐ๊ฑด

  • N๊ณผ K๋Š” ์ •์ˆ˜์ด๋‹ค.

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

3์‹œ๊ฐ„

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

1. ๋ฌธ์ œ ์ง€๋ฌธ ์ฝ๊ธฐ

  • ์œ„์น˜๊ฐ€ X์ผ ๋•Œ ๊ฑท๋Š”๋‹ค๋ฉด 1์ดˆ ํ›„์— X-1 ๋˜๋Š” X+1๋กœ ์ด๋™ํ•œ๋‹ค.
  • ์ˆœ๊ฐ„์ด๋™์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” 0์ดˆ ํ›„์— 2*X์˜ ์œ„์น˜๋กœ ์ด๋™ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋„๋‹ฌํ•˜๋Š” ์‹œ๊ฐ„์ด ๋ช‡ ์ดˆ์ธ์ง€ ์ฐพ๋Š”๋‹ค.

2. bfs 1์ฐจ ์‹œ๋„ (๋ฌดํ•œ ์‚ฝ์งˆ์˜ ์„œ๋ง‰)

2๋‹ฌ์ „ ์‹œ๋„ํ–ˆ๋‹ค๊ฐ€ ๊ด‘ํƒˆ๋‹นํ•œ ๋ฌธ์ œ๋ฅผ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉฐ ๋‹ค์‹œ ๋“ค์—ฌ๋‹ค๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.
bfs๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ์ง€๋งŒ ๊ณ„์†ํ•ด์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋‚˜์™€์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค.
๊ทผ๋ฐ ์ด๊ฒŒ ์›ฌ๊ฑธ? ์•„๋ฌด๋ฆฌ ๋ด๋„ ์ฐจ์ด๊ฐ€ ์—†๋Š” ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ ์•„์ฃผ ํฌ๋‚˜ํฐ ์ฐจ์ด๊ฐ€ ์žˆ์—ˆ๋‹ค.

image

x + 1, x - 1 ์ˆœ์„œ์— ๋”ฐ๋ผ์„œ ๋‹ต์ด ๋‹ฌ๋ผ์ง€๋Š” ์ด์œ 
image

  • ๊ฐ„์„ ์˜ ๊ธธ์ด๊ฐ€ ๋‹ค์–‘ํ•˜๊ธฐ์— ์ฒ˜์Œ ์ฐพ์€ ๊ฒฝ๋กœ๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ์Œ์„ ๊ฐ„๊ณผํ•˜์˜€๋‹ค.
  • ํ˜„์žฌ ์œ„์น˜์—์„œ 2๋ฐฐ ์ ํ”„ํ•œ ๊ฒฝ์šฐ (๊ฐ€์ค‘์น˜ 0) ๋ณด๋‹ค ๋นจ๋ฆฌ K์— ๋„๋‹ฌํ•˜๊ฒŒ ๋˜๋ฉด ์˜ค๋‹ต์ด ๋œ๋‹ค๋Š” ์ .
        for nx in [cx*2, cx+1, cx-1]: # +1๋กœ *2๋ณด๋‹ค ๋น ๋ฅด๊ฒŒ ๋„๋‹ฌํ•  ๊ฒฝ์šฐ ์˜ค๋‹ต 
            if (0 <= nx <= 100000) and board[nx] == -1:
                queue.append(nx)
                board[nx] = board[cx] + (0 if nx == cx*2 else 1)

-> ๊ฐฑ์‹  ์กฐ๊ฑด์„ โ€œ-1์ด ์•„๋‹ˆ๋ฉดโ€œ์ด ์•„๋‹ˆ๋ผ ์—ญ๋Œ€ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๊ฐ€๊นŒ์šฐ๋ฉด์œผ๋กœ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.
ํŠน์ •ํ•œ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•˜๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•˜๋‹ค.

image

3. ๋‹ค์ต์ŠคํŠธ๋ผ ํ’€์ด

๋‹ค์ต์ŠคํŠธ๋ผ ์ตœ๋‹จ ๊ฒฝ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ž€?

Dijkstra_Animation
์ถœ์ฒ˜

๐Ÿ’ก ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์›๋ฆฌ

  1. ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
  2. ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งฆ์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•œ๋‹ค.
  4. ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ฑฐ์ณ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
  5. 3๋ฒˆ๊ณผ 4๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

-> ์ž„์˜์˜ ๋…ธ๋“œ X์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ X-1, X+1, X*2 ์‚ฌ์ด์˜ ์ตœ์†Œ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•œ๋‹ค.
์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ ๊ฐ ๋…ธ๋“œ์— ๋Œ€ํ•œ ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฐ€์žฅ ์ตœ๋‹จ์˜ ๊ฑฐ๋ฆฌ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด๋‚˜๊ฐ„๋‹ค.
ํ˜„์žฌ ์ฒ˜๋ฆฌํ•˜๊ณ  ์žˆ๋Š” ๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋” ์งง์€ ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค? ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๊ฒฝ๋กœ๋ฅผ ๋” ์งง์€ ๊ฒฝ๋กœ๋ผ๊ณ  ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
image

-> ์ด๋ ‡๊ฒŒ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰ ํ›„ N์—์„œ K๋กœ ๊ฐ€๋Š” ์ตœ์†Œ ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿค” ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฑฐ๋ฆฌ ์ˆœ์„œ๋กœ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ์ •๋ ฌํ•˜๊ธฐ

ํ†ต์ƒ์ ์œผ๋กœ ์กฐ๊ธˆ ๋” ๋น ๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๋Š” heapq๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ์‹
๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๊ฐ€ N๊ฐœ์ผ ๋•Œ, ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œํ•  ๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(logN)

๊ฐœ์„ ๋œ ๋‹ค์ต์ŠคํŠธ๋ผ

  • ํŒŒ์ด์ฌ์˜ heapq ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉ
  • ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌ์„ฑ
  • ๋”ฐ๋ผ์„œ (๊ฑฐ๋ฆฌ, ์ด๋™ํ•  ๋…ธ๋“œ ๋ฒˆํ˜ธ) ์ˆœ์„œ๋กœ ํŠœํ”Œ์„ ๊ตฌ์„ฑํ•˜์—ฌ ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ๊ฑฐ๋ฆฌ ์ˆœ์œผ๋กœ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ฐพ์Œ
  • ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅ
import heapq
...

def dijkstra(start):
    q = []
    # ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์ „ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ 0์œผ๋กœ ์„ค์ •, ํ ์‚ฝ์ž…
    heapq.heappush(q,(0,start))
    board[start] = 0
  • ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ์›์†Œ๋ฅผ ๊บผ๋‚ด ๊ฐ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ์ตœ์†Œ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.
  • ๋” ์งง์€ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•˜์„ ๋•Œ, ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ” ๊ฐฑ์‹  + ๋…ธ๋“œ์˜ ์ •๋ณด๋ฅผ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค.
  • ์œ„ ๋‹จ๊ณ„๋ฅผ ๋ฐ˜๋ณตํ•œ ํ›„ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ํ…Œ์ด๋ธ”์— ๋‚จ์•„ ์žˆ๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
 while q:
        dist, now = heapq.heappop(q)

        # ์ด๋ฏธ ๊ฐฑ์‹ ๋œ ์ ์ด ์žˆ๋Š” ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ ๋ฌด์‹œ
        if board[now] < dist:
            continue

        for jump, time in [(now*2, dist),(now+1, dist+1),(now-1, dist+1)]:
            if not OOB(jump) and board[jump] > time:
                board[jump] = time
                heapq.heappush(q,(time,jump))

4. ์ „์ฒด ์ฝ”๋“œ

import heapq
import sys
input = sys.stdin.readline

INF = float('inf')
n, k = map(int,input().split())

# ์‹œ์ž‘ ์œ„์น˜ ์ดˆ๊ธฐํ™”
board = [INF] * 100001
def OOB(x):
    return not(0 <= x <= 100000)

# ๋‹ค์ต์ŠคํŠธ๋ผ
def dijkstra(start):
    q = []
    # ์‹œ์ž‘ ๋…ธ๋“œ๋กœ ๊ฐ€๊ธฐ ์ „ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ 0์œผ๋กœ ์„ค์ •, ํ ์‚ฝ์ž…
    heapq.heappush(q,(0,start))
    board[start] = 0

    while q:
        dist, now = heapq.heappop(q)

        # ์ด๋ฏธ ๊ฐฑ์‹ ๋œ ์ ์ด ์žˆ๋Š” ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ ๋ฌด์‹œ
        if board[now] < dist:
            continue

        for jump, time in [(now*2, dist),(now+1, dist+1),(now-1, dist+1)]:
            if not OOB(jump) and board[jump] > time:
                board[jump] = time
                heapq.heappush(q,(time,jump))

dijkstra(n)
print(board[k])
from collections import deque

def bfs(n,k):
    queue = deque([n])
    board[n] = 0

    while queue:
        cx = queue.popleft()

        if cx == k:
            return ;
        for nx in [cx*2, cx+1, cx-1]:
            if (0 <= nx <= 100000) and board[nx] == -1:
                queue.append(nx)
                board[nx] = board[cx] + (0 if nx == cx*2 else 1)

n, k = map(int,input().split())
board = [-1] * 100001
bfs(n, k)
print(board[k])

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

์ฐธ๊ณ 

@Munbin-Lee Munbin-Lee closed this Feb 11, 2024
@Munbin-Lee Munbin-Lee changed the title 14-LJEDD2 test Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants