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

728x90
๋ฐ˜์‘ํ˜•

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

 

๋ฌธ์ œ

 

12851๋ฒˆ: ์ˆจ๋ฐ”๊ผญ์งˆ 2

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์•ž์„œ ๋‹ค๋ฃฌ 1697 ์ˆจ๋ฐ”๊ผญ์งˆ ๋ฌธ์ œ์—์„œ ์กฐ๊ฑด์ด ์ถ”๊ฐ€๋œ ๋ฌธ์ œ์ด๋‹ค. ์ˆ˜๋นˆ์ด์˜ ์œ„์น˜๊ฐ€ 5, ๋™์ƒ์˜ ์œ„์น˜๊ฐ€ 17์ผ ๋•Œ BFS๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰ํ•ด๋ณด๋ฉด ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„์ธ 4์ดˆ์— ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€ ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 ์ฆ‰, ์ด์ „ ๋ฌธ์ œ๋Š” ๋‹จ์ˆœํžˆ ๋ช‡ ์ดˆ๋งŒ์— ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”๊ฐ€?๋ฅผ ๋ฌป๋Š” ๋ฌธ์ œ์˜€์œผ๋ฉด ๊ฐ€์žฅ ๋น ๋ฅธ ์‹œ๊ฐ„์— ์ฐพ๋Š” ๋ฐฉ๋ฒ•์€ ๋ช‡ ๊ฐ€์ง€์ธ์ง€๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์ฐพ๋Š” ๋ฌธ์ œ์ด๋‹ค. 

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


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

    while q:
        cur_loc = q.popleft()

        for next_loc in (cur_loc - 1, cur_loc + 1, cur_loc * 2):
            if 0 <= next_loc < MAX_LOC:
                if not check[next_loc][VISIT]:
                    check[next_loc][VISIT] = check[cur_loc][VISIT] + 1
                    check[next_loc][WAY] = check[cur_loc][WAY]
                    q.append(next_loc)
                elif check[next_loc][VISIT] == check[cur_loc][VISIT] + 1:
                    check[next_loc][WAY] += check[cur_loc][WAY]

    return check[k]


if __name__ == '__main__':
    MAX_LOC = 100001
    VISIT, WAY = 0, 1
    n, k = map(int, stdin.readline().split())
    check = [[0, 0] for _ in range(MAX_LOC)]
    check[n][WAY] = 1
    time, way = bfs(n)
    print(time, way, sep='\n')

1697 ์ˆจ๋ฐ”๊ผญ์งˆ ๋ฌธ์ œ์™€ ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ์œ„์น˜๊นŒ์ง€ ๋ฐฉ๋ฌธํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๊ณผ, ๋งŒ์•ฝ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๋ผ๋ฉด ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๊ณผ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ, ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€