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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

12869๋ฒˆ: ๋ฎคํƒˆ๋ฆฌ์Šคํฌ

1, 3, 2 ์ˆœ์„œ๋Œ€๋กœ ๊ณต๊ฒฉ์„ ํ•˜๋ฉด, ๋‚จ์€ ์ฒด๋ ฅ์€ (12-9, 10-1, 4-3) = (3, 9, 1)์ด๋‹ค. 2, 1, 3 ์ˆœ์„œ๋Œ€๋กœ ๊ณต๊ฒฉ์„ ํ•˜๋ฉด, ๋‚จ์€ ์ฒด๋ ฅ์€ (0, 0, 0)์ด๋‹ค.

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ๋ฎคํƒˆ๋ฆฌ์Šคํฌ๋Š” ํ•œ ๋ฒˆ์— 3๋งˆ๋ฆฌ์˜ SCV๋ฅผ ๊ณต๊ฒฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๊ณต๊ฒฉ์€ 9, ๋‘ ๋ฒˆ์งธ ๊ณต๊ฒฉ์€ 3, ์„ธ ๋ฒˆ์งธ ๊ณต๊ฒฉ์€ 1๋กœ ์ฒด๋ ฅ์„ ๊ฐ์†Œ์‹œํ‚ฌ ๋•Œ, ๋ชจ๋“  SCV์˜ ์ฒด๋ ฅ์ด 0์ด ๋˜๋Š” ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฝ์šฐ์˜ ๊ณต๊ฒฉ ํšŸ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

DFS๋ฅผ ํ†ตํ•ด์„œ๋„ ํ’€ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด BFS๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜์—ฌ ๋ฌธ์ œ์˜ ๋‹ต์„ ์ฐพ์•˜๋‹ค.

  • (9, 3, 1)๋กœ ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ํŒจํ„ด์„ `permutation`์„ ํ™œ์šฉํ•˜์—ฌ ๊ตฌํ•œ๋‹ค.
  • BFS๋ฅผ ํ†ตํ•ด ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ํŒจํ„ด๋งŒํผ ๊ฐ€์ง€๋ฅผ ํ™•์žฅํ•ด ๋‚˜๊ฐ„๋‹ค.
    • ์ด๋•Œ, scv3์˜ ์ฒด๋ ฅ์ด ๊ฐ€์žฅ ๋†’์€ ์ƒํƒœ๋กœ ์ •๋ ฌํ•ด์„œ ๋‚˜๊ฐ„๋‹ค.
  • ์žฌ๋ฐฉ๋ฌธ์„ ๋ง‰๊ธฐ ์œ„ํ•ด `visited`๋ฅผ 3์ฐจ์› ๋ฐฐ์—ด๋กœ ์ฒด๋ ฅ์˜ ํฌ๊ธฐ๋งŒํผ ๋งŒ๋“ค์–ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•œ๋‹ค.
  • scv๋“ค์˜ ์ฒด๋ ฅ์€ ์ •๋ ฌ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ, ์„ธ ๋ฒˆ์งธ scv์˜ ์ฒด๋ ฅ์ด 0์ด ๋œ๋‹ค๋ฉด BFS ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from itertools import permutations
from collections import deque


def bfs():
    q = deque([[*scv, 0]])

    while q:
        cur_state = q.popleft()

        # ์ •๋ ฌ๋œ ์ƒํƒœ์—์„œ ์ฒด๋ ฅ์ด ํฐ SCV์˜ ์ฒด๋ ฅ์ด ์—†๋‹ค๋ฉด ์ข…๋ฃŒ.
        if cur_state[THREE] == 0:
            return cur_state[CNT]

        # ๊ณต๊ฒฉ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋งŒํผ ํ์— ์ถ”๊ฐ€.
        for case in attack_case:
            next_scv = [0] * 3

            # visited์— ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด 0๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ 0์œผ๋กœ ๋ณ€ํ™˜.
            for i in range(3):
                next_scv[i] = \
                    cur_state[i] - case[i] if cur_state[i] - case[i] > 0 else 0

            # ์ •๋ ฌ๋œ ์ƒํƒœ๋กœ ํ์— ์ถ”๊ฐ€.
            next_scv.sort()

            if not visited[next_scv[ONE]][next_scv[TWO]][next_scv[THREE]]:
                visited[next_scv[ONE]][next_scv[TWO]][next_scv[THREE]] = True
                q.append([*next_scv, cur_state[CNT] + 1])


if __name__ == '__main__':
    ONE, TWO, THREE, CNT = 0, 1, 2, 3
    n = int(stdin.readline())
    # n์ด 3๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ, 0์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด n์ด 1, 2์ธ ๊ฒฝ์šฐ ๋ณ„๋„๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋จ.
    scv = sorted(list(map(int, stdin.readline().split())) + (3 - n) * [0])
    attack_case = list(permutations([9, 3, 1], 3))
    visited = [[[False] * 61 for _ in range(61)] for _ in range(61)]
    print(bfs())
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€