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

20-alstjr7437 #71

Merged
merged 9 commits into from
Apr 10, 2024
Merged

20-alstjr7437 #71

merged 9 commits into from
Apr 10, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Apr 3, 2024

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

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

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

30๋ถ„



๐Ÿ“œ ๋ฌธ์ œ ์„ค๋ช…

  1. n๊ณผ k๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
  2. ์ˆซ์ž n์€ -1, +1, *2๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  3. n์—์„œ k๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋ช‡๋ฒˆ ๊ฑธ๋ฆฌ๋Š”์ง€ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ) n -> 5 k -> 17
n : 5 -> 10 -> 9 -> 18 -> 17





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

์˜ค๋ž˜๋งŒ์— ๋ฐฑ์ค€ ์•„๋ž˜์— ์žˆ๋Š” ๋ฌธ์ œ ์œ ํ˜•์„ ์•ˆ๋ณด๊ณ  ํ’€์—ˆ๋Š”๋ฐ
์ฒ˜์Œ์— ์‚ฌ์‹ค ์•„์นจ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—…๋“ค์œผ๋ฉฐ ๋ฌธ์ œ ์„ ์ •ํ•˜๋ฉด์„œ ์˜ค์ž‰ DP ๋ฌธ์ œ ์•„๋‹Œ๊ฐ€ ์ƒ๊ฐํ•ด๋†“๊ณ 




๋‹ค๋ฅธ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ํ‹ˆ์ด ๋‚ ๋•Œ(๊ธธ ๊ฑธ์„๋•Œ ๋“ฑ๋“ฑ..) ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค!!




์ผ๋‹จ ์‹ฌ์‹ฌํ• ๋•Œ ๋„์ ์ธ๊ฑธ๋กœ๋Š” ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด
image



-1, +1, *2๋กœ 3๊ฐˆ๋ž˜๋กœ ๋‚˜๋ˆ ์ง€๋ฉด์„œ
๋งŒ์•ฝ์— k๋ผ๋Š” ๋ถ€๋ถ„์— ์˜ค๊ฒŒ ๋˜๋ฉด
ํ•ด๋‹น ๋ถ€๋ถ„์˜ Level์„ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๊ตฌ๋‚˜ ํ•˜๋ฉด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!!


๊ทธ๋ฆฌ๊ณ  ๋ฌดํ•œ ๋ฃจํ”„ ๋ฐ ์‹œ๊ฐ„์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์ค‘๋ณต์œผ๋กœ ๋ฐฉ๋ฌธํ•˜๋Š” ์ˆซ์ž๋Š” Level๋กœ visited ์ฒ˜๋ฆฌ ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.





๐Ÿ’ป ์ตœ์ข… ์ฝ”๋“œ

from collections import deque

n, k = map(int, input().split())

result = [0] * 100001
result[n] = 1

queue = deque()
queue.append(n)

while queue:
    now = queue.popleft()
    if now == k:
        break
    # -1 ๋กœ์ง
    if now - 1 >= 0 and result[now-1] == 0 :
        queue.append(now-1)
        result[now-1] = result[now] + 1
    # +1 ๋กœ์ง
    if now + 1 < len(result) and result[now+1] == 0:
        queue.append(now+1)
        result[now+1] = result[now] + 1
    # *2 ๋กœ์ง
    if now * 2 < len(result) and result[now*2] == 0:
        queue.append(now*2)
        result[now*2] = result[now] + 1

print(result[k] - 1)

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

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์ด์ œ ๊ทธ๋ž˜ํ”„๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š”์ง€ ์กฐ๊ธˆ์”ฉ ๊ฐ์„ ์žก์•„๊ฐ€๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค!!!

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

ํ—‰ ๋„ค์ด๋ฒ„ ์ฝ”ํ…Œ ์ง์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ๋ผ ๋‹ค์‹œ ํ’€์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค..ํ•˜ํ•˜
์ €๋Š” bfs ๋กœ ํƒ์ƒ‰ํ•˜๊ณ  ์ฐพ์œผ๋ฉด return ํ•˜๋„๋ก ํ’€์—ˆ๋Š”๋ฐ ํŠธ๋ฆฌ๋กœ ํ‘ธ์…จ๊ตฐ์š”?
์ƒ๊ฐ์ง€ ๋ชปํ•œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ• ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค..!

val max = 100_001
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val visited = MutableList(max) { 0 }
    val queue = ArrayDeque<Int>()
    val (x, y) = br.readLine().split(" ").map { it.toInt() }
    queue.add(x)
    visited[x] = 1

    while (queue.isNotEmpty()) {
        val now = queue.removeFirst()
        if (now == y) {
            println(visited[now]-1)
            return
        }

        val nows = mutableListOf<Int>()

        nows.add(now + 1)
        nows.add(now - 1)
        nows.add(2 * now)

        for (i in nows.indices) {
            if (nows[i] > max - 1 || nows[i] < 0) {
                continue
            }
            if (visited[nows[i]] == 0) {
                visited[nows[i]] = visited[now] + 1
                queue.add(nows[i])
            }
        }
    }
}

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

์ฒ˜์Œ์— ์–ด ์™œ ๊ตณ์ด result ๋ฅผ ๋”ฐ๋กœ ๋นผ์„œ ์ด๋™ ์‹œ๊ฐ„์„ ์ €์žฅํ•˜์ง€ ํ•˜๊ณ  ๊ทธ๋ƒฅ ์Œ์„ q์— ์ €์žฅํ–ˆ๋Š”๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋„ค์š”..!

# ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ ์ฝ”๋“œ
import sys
input=sys.stdin.readline
from collections import deque

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

q=deque()
q.append((N,0))
result = 0
while q:
    current,second = q.popleft()
    if current==K:
        result = second
        break
    
    q.append((current-1, second+1))
    q.append((current+1, second+1))
    q.append((current*2, second+1))

print(result)

์•Œ๊ณ ๋ณด๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ์ด 128MB ์˜€๊ณ  ์–ด๋งˆ๋ฌด์‹œํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒ ๊ฑฐ๋‹ˆ ํ•˜๊ณ  ์ˆœ์ˆœํžˆ ๋ฏผ์„ ๋‹˜์ฒ˜๋Ÿผ ํ’€์—ˆ์Šด๋‹ค..

import sys
input=sys.stdin.readline
from collections import deque

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

q=deque()
q.append(N)
result = [0] * 100001

while q:
    current = q.popleft()
    if current==K:
        break
    
    if current-1 >=0 and result[current-1] ==0:
        result[current-1] = result[current] + 1    
        q.append(current-1)

    if current+1 <=100000 and result[current+1] ==0:
        result[current+1] = result[current] + 1    
        q.append(current+1)

    if current*2 <=100000 and result[current*2] ==0:
        result[current*2] = result[current] + 1    
        q.append(current*2)

print(result[K])

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

ํŠธ๋ฆฌ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๊ตฐ์š”..
์ €๋Š” ์„ฑํ›ˆ๋‹˜์ฒ˜๋Ÿผ BFS๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค! ์˜ํฌ๊ฐ€ 0~100,000 ์‚ฌ์ด์— ์žˆ์„ ์ˆ˜ ์žˆ์–ด์„œ
Dist ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๋ฅผ 10**6์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ  ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

from collections import deque


import sys

input = sys.stdin.readline
INF=10**6

def bfs():
    q = deque()
    q.append(N)
    while q:
        x = q.popleft()
        if x == M:
            print(dist[x])
            break
        for nx in (x-1, x+1, x*2):
            if 0 <= nx < INF and dist[nx] == 0:
                dist[nx] = dist[x] + 1
                q.append(nx)

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

dist = [0] * (INF)

bfs()

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