๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 16397 - ํƒˆ์ถœ

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 16397๋ฒˆ

ํ’€์ด ๊ณผ์ •

BFS ๋ฅผ ํ†ตํ•ด ๋ชฉํ‘œ ์ˆซ์ž์— ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ๋ฒ„ํŠผ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๊ฐ๊ฐ์˜ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์ด ์ฃผ์–ด์ ธ ์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.
์ˆซ์ž์— ๋Œ€ํ•œ ์—ฐ์‚ฐ(๋งจ ์•ž์ž๋ฆฌ ๊ฐ์†Œ)์€ Python์˜ ์Šฌ๋ผ์ด์‹ฑ์„ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque


N, T, G = list(map(int, sys.stdin.readline().split()))
INF = 100000


def bfs(start):
    q = deque()
    visit = [0 for _ in range(INF)]
    q.append((start, 0))
    visit[start] = 1

    while q:
        here, cost = q.popleft()
        if cost > T:
            return 'ANG'
        if here == G:
            return cost

        # A ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ
        if here+1 < INF and not visit[here+1]:
            q.append((here+1, cost+1))
            visit[here+1] = 1

        # B ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ
        if here*2 < INF:
            temp = str(here*2)
            if int(temp) != 0:
                temp = str(int(temp[0])-1) + temp[1:]
            if not visit[int(temp)]:
                q.append((int(temp), cost+1))
                visit[int(temp)] = 1

    return 'ANG'


def solution():
    ans = bfs(N)
    return ans


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 2178 - ๋ฏธ๋กœ ํƒ์ƒ‰  (0) 2021.03.18
BOJ 6593 - ์ƒ๋ฒ” ๋นŒ๋”ฉ  (0) 2021.03.18
BOJ 2563 - ์ƒ‰์ข…์ด  (0) 2021.03.18
BOJ 1963 - ์†Œ์ˆ˜ ๊ฒฝ๋กœ  (0) 2021.03.18
BOJ 4195 - ์นœ๊ตฌ ๋„คํŠธ์›Œํฌ  (0) 2021.03.18

๐Ÿ’ฌ ๋Œ“๊ธ€