ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

์ˆจ๋ฐ”๊ผญ์งˆ ์‹œ๋ฆฌ์ฆˆ

 

๋ฌธ์ œ

 

13913๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 4

์ˆ˜๋นˆ์ด๋Š” ๋™์ƒ๊ณผ ์ˆจ๋ฐ”๊ผญ์งˆ์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ํ˜„์žฌ ์  N(0 ≤ N ≤ 100,000)์— ์žˆ๊ณ , ๋™์ƒ์€ ์  K(0 ≤ K ≤ 100,000)์— ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๊ฑท๊ฑฐ๋‚˜ ์ˆœ๊ฐ„์ด๋™์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ, ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ X์ผ ๏ฟฝ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 1697 ์ˆจ๋ฐ”๊ผญ์งˆ ๋ฌธ์ œ์—์„œ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ช‡ ์ดˆ ๋งŒ์— ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ 2์ฐจ์› ๋ฐฐ์—ด์„ ํ†ตํ•ด, ๋ฐฉ๋ฌธ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ๋กํ•ด๋‘๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ์‹œ์—์„œ๋Š” ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ ๋ฌธ์ œ์— ๋Œ€ํ•ด 2๊ฐ€์ง€ ๊ฒฝ๋กœ๋ฅผ ์ถœ๋ ฅํ•˜์˜€์ง€๋งŒ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋งŒ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅํ•˜์—ฌ๋„ ๋‹ต์œผ๋กœ ์ฒ˜๋ฆฌ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def bfs(start):
    q = deque([start])

    while q:
        cur_loc = q.popleft()
        if cur_loc == k:
            return visited[cur_loc][TIME]

        for next_loc in (cur_loc - 1, cur_loc + 1, cur_loc * 2):
            if 0 <= next_loc < MAX_LOC and not visited[next_loc][TIME]:
                visited[next_loc][TIME] = visited[cur_loc][TIME] + 1
                visited[next_loc][PATH] = cur_loc
                q.append(next_loc)


if __name__ == '__main__':
    MAX_LOC, TIME, PATH = 100001, 0, 1
    n, k = map(int, stdin.readline().split())
    visited = [[0, 0] for _ in range(MAX_LOC)]
    print(bfs(n))

    answer = [k]
    while k != n:
        k = visited[k][PATH]
        answer.append(k)

    print(*answer[::-1])
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€