[BOJ][๐ŸŸก4][๋ฐฑ์ค€#12869] ๋ฎคํƒˆ๋ฆฌ์Šคํฌ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #12869


๋ฌธ์ œ

์ˆ˜๋นˆ์ด๋Š” ๊ฐ•ํ˜ธ์™€ ํ•จ๊ป˜ ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ ๊ฒŒ์ž„์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์ˆ˜๋นˆ์ด๋Š” ๋ฎคํƒˆ๋ฆฌ์Šคํฌ 1๊ฐœ๊ฐ€ ๋‚จ์•„์žˆ๊ณ , ๊ฐ•ํ˜ธ๋Š” SCV N๊ฐœ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค. ๊ฐ๊ฐ์˜ SCV๋Š” ๋‚จ์•„์žˆ๋Š” ์ฒด๋ ฅ์ด ์ฃผ์–ด์ ธ์žˆ์œผ๋ฉฐ, ๋ฎคํƒˆ๋ฆฌ์Šคํฌ๋ฅผ ๊ณต๊ฒฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ์ฆ‰, ์ด ๊ฒŒ์ž„์€ ์ˆ˜๋นˆ์ด๊ฐ€ ์ด๊ฒผ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋ฎคํƒˆ๋ฆฌ์Šคํฌ๊ฐ€ ๊ณต๊ฒฉ์„ ํ•  ๋•Œ, ํ•œ ๋ฒˆ์— ์„ธ ๊ฐœ์˜ SCV๋ฅผ ๊ณต๊ฒฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ๋กœ ๊ณต๊ฒฉ๋ฐ›๋Š” SCV๋Š” ์ฒด๋ ฅ 9๋ฅผ ์žƒ๋Š”๋‹ค. ๋‘ ๋ฒˆ์งธ๋กœ ๊ณต๊ฒฉ๋ฐ›๋Š” SCV๋Š” ์ฒด๋ ฅ 3์„ ์žƒ๋Š”๋‹ค. ์„ธ ๋ฒˆ์งธ๋กœ ๊ณต๊ฒฉ๋ฐ›๋Š” SCV๋Š” ์ฒด๋ ฅ 1์„ ์žƒ๋Š”๋‹ค.

SCV์˜ ์ฒด๋ ฅ์ด 0 ๋˜๋Š” ๊ทธ ์ดํ•˜๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋ฉด, SCV๋Š” ๊ทธ ์ฆ‰์‹œ ํŒŒ๊ดด๋œ๋‹ค. ํ•œ ๋ฒˆ์˜ ๊ณต๊ฒฉ์—์„œ ๊ฐ™์€ SCV๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ๊ณต๊ฒฉํ•  ์ˆ˜๋Š” ์—†๋‹ค. ๋‚จ์•„์žˆ๋Š” SCV์˜ ์ฒด๋ ฅ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ชจ๋“  SCV๋ฅผ ํŒŒ๊ดดํ•˜๊ธฐ ์œ„ํ•ด ๊ณต๊ฒฉํ•ด์•ผ ํ•˜๋Š” ํšŸ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— SCV์˜ ์ˆ˜ N (1 โ‰ค N โ‰ค 3)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” SCV N๊ฐœ์˜ ์ฒด๋ ฅ์ด ์ฃผ์–ด์ง„๋‹ค. ์ฒด๋ ฅ์€ 60๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ชจ๋“  SCV๋ฅผ ํŒŒ๊ดดํ•˜๊ธฐ ์œ„ํ•œ ๊ณต๊ฒฉ ํšŸ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

3
12 10 4


์ถœ๋ ฅ

2


์˜ˆ์ œ 2

์ž…๋ ฅ

3
54 18 6


์ถœ๋ ฅ

6


์˜ˆ์ œ 3

์ž…๋ ฅ

1
60


์ถœ๋ ฅ

7


์˜ˆ์ œ 4

์ž…๋ ฅ

3
1 1 1


์ถœ๋ ฅ

1


์˜ˆ์ œ 5

์ž…๋ ฅ

2
60 40


์ถœ๋ ฅ

9


My Sol

from collections import deque

def main():
    N = int(input())
    scvs = list(map(int, input().split()))
    Q = deque([(scvs, N, 0)])
    watched = set()
    damages = [
        [],
        [[9]],
        [[9,3], [3,9]],
        [[9,3,1],[9,1,3],[3,9,1],[3,1,9],[1,9,3],[1,3,9]]
    ]

    while Q:
        cur_scvs, scvs_cnt, attack_cnt = Q.popleft()
        if str(cur_scvs) in watched: continue
        watched.add(str(cur_scvs))

        for damage in damages[scvs_cnt]:
            attacked_scvs = []
            kill_cnt = 0
            for n in range(scvs_cnt):
                attacked_scv = cur_scvs[n] - damage[n]
                if attacked_scv <= 0:
                    kill_cnt += 1
                else:
                    attacked_scvs.append(attacked_scv)
            if kill_cnt == scvs_cnt: return attack_cnt+1
            Q.append((attacked_scvs, scvs_cnt-kill_cnt, attack_cnt+1))

print(main())

๊ต‰์žฅํžˆ ์‰ฝ๊ฒŒ BFS๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค.

  1. ์ฒ˜์Œ์— SCV์˜ ๊ฐœ์ˆ˜์™€ ์ฒด๋ ฅ ์ •๋ณด๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๋Š”๋‹ค.
  2. deque Q์— (SCV์˜ ์ฒด๋ ฅ ์ •๋ณด ๋ฆฌ์ŠคํŠธ, SCV ๊ฐœ์ˆ˜, ๊ณต๊ฒฉํšŸ์ˆ˜)๋ฅผ ๋„ฃ๋Š”๋‹ค.
  3. ๊ฐ™์€ ์ฒด๋ ฅ์— ๋Œ€ํ•œ ๊ณต๊ฒฉ์„ ์ง„ํ–‰ํ–ˆ์„ ๊ฒฝ์šฐ ๊ฐ™์€ ๊ณต๊ฒฉ์„ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๋„๋ก ์‚ฌ์‹ค์ƒ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๋”ฐ์ง€๋Š” watched set์„ ๋‘”๋‹ค.
  4. ๊ฐ SCV๋“ค์˜ ์ˆ˜์— ๋”ฐ๋ผ ๊ณต๊ฒฉ์˜ ํŒจํ„ด์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” damages 2์ฐจ์› ๋ฐฐ์—ด์„ ๋‘”๋‹ค.
  5. deque Q์—์„œ SCV ๋ฐ ๊ณต๊ฒฉ ์ •๋ณด๋ฅผ ํ•˜๋‚˜์”ฉ ๋นผ๋ฉด์„œ ์กฐํšŒํ•˜๋Š”๋ฐ, SCV์˜ ์ฒด๋ ฅ ์ •๋ณด๋ฅผ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌํ•œ ๊ฒƒ์ด watched์— ์กด์žฌํ•œ๋‹ค๋ฉด continueํ•œ๋‹ค. ์ด๋•Œ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌํ•œ ์ด์œ ๋Š” list๋Š” set์—์„œ hashํ™”ํ•  ์ˆ˜ ์—†๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋•Œ๋ฌธ์— ๊ทธ ์ž์ฒด๋กœ string ์ฒ˜๋ฆฌํ•˜์—ฌ ์ €์žฅํ•˜๊ณ  ์กฐํšŒํ–ˆ๋‹ค. watched์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ์ฒซ ์ฒด๋ ฅ ๊ตฌ์กฐ์ธ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— watched์— ํ˜„์žฌ ์ฒด๋ ฅ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ๊ณต๊ฒฉ์„ ์‹œ์ž‘ํ•œ๋‹ค.
  6. SCV์˜ ์ˆ˜์— ๋”ฐ๋ฅธ ๊ณต๊ฒฉ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ for๋ฌธ์œผ๋กœ ๋ฐ˜๋ณต์ฒ˜๋ฆฌํ•œ๋‹ค. ํ˜„์žฌ SCV ์ฒด๋ ฅ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์ฒด๋ ฅ๋“ค์— ๋Œ€ํ•ด ๊ณต๊ฒฉ์„ ์ง„ํ–‰ํ•˜๊ณ  ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋œ SCV๋“ค์˜ ์ˆ˜๋ฅผ ์„ผ๋‹ค. ๊ณต๊ฒฉ์„ ๋งˆ์น˜๊ณ  ์ด ์ˆซ์ž๊ฐ€ ๊ณต๊ฒฉ ์ „ SCV์˜ ์ˆ˜์™€ ๊ฐ™๋‹ค๋ฉด ๋ชจ๋“  SCV๊ฐ€ ํŒŒ๊ดด๋œ ๊ฒƒ์ด๋ฏ€๋กœ attack_cnt์— 1์„ ๋”ํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ์•„์ง SCV๊ฐ€ ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์ด๊ณ , ์ด๋•Œ์˜ SCV ์ฒด๋ ฅ์€ attacked_scvs์— ์ €์žฅ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ์ด๋ฅผ Q์— ๋„ฃ์–ด ๋‹ค์Œ ๊ณต๊ฒฉ์— ์‚ฌ์šฉํ•œ๋‹ค. ์ด๋•Œ SCV์˜ ์ฒด๋ ฅ๋“ค์˜ watched ์ฒ˜๋ฆฌ๋ฅผ ์ œ๋Œ€๋กœ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ์ฒด๋ ฅ๋“ค์€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค. ๋งŒ์•ฝ ์ •๋ ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ™์€ ์ฒด๋ ฅ ๊ตฌ์„ฑ์ด๋”๋ผ๋„ ์ˆœ์„œ๊ฐ€ ๋‹ฌ๋ผ watched๋ฅผ ํ†ต๊ณผํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์‹ค์ œ๋กœ sort ๊ณผ์ •์„ ํ†ตํ•ด ์—ฐ์‚ฐ์†๋„๊ฐ€ 10% ํ–ฅ์ƒ๋˜์—ˆ์Œ์„ ํ™•์ธํ–ˆ๋‹ค.
  7. main ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


์ฒ˜์Œ์—๋Š” ์•„๋ž˜์˜ itertools์˜ ์ˆœ์—ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด damages ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

from itertools import permutations
def make_damages():
  damages = [9, 3, 1]
  D = [[] for _ in range(4)]
  for n in range(1, 4):
      D[n] = [lst for lst in permutations(damages[:n], n)]
  return D

damages = make_damages()

๊ทธ๋Ÿฐ๋ฐ ์ˆซ์ž๊ฐ€ ๊ทธ๋‹ค์ง€ ๋งŽ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ ํ•˜๋“œ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๊ณ , ์‹œ๊ฐ„์ƒ ์ฐจ์ด๋Š” ๋งŽ์ด ์—†์—ˆ์œผ๋‚˜ ์ฝ”๋“œ ๊ธธ์ด๋ฅผ 100B ๊ฐ€๋Ÿ‰ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ