Post

[BOJ] 16236. ์•„๊ธฐ์ƒ์–ด(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. ์•„๊ธฐ์ƒ์–ด ์œ„์น˜๊ตฌํ•˜๊ธฐ
    ์•„๊ธฐ์ƒ์–ด์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•˜๊ณ  board์— ์žˆ๋Š” ์ˆซ์ž ํฌ๊ธฐ๋กœ ์ด๋™ํ•  ์ˆ˜์žˆ๋Š”์ง€ ์ฒดํฌํ•  ๋•Œ ํ˜ผ๋™์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ์•„๊ธฐ์ƒ์–ด์œ„์น˜์— 0์„ ๋Œ€์ž…ํ•œ๋‹ค.

  2. ์‚ฌ์ด์ฆˆ๋ณ„ ๋ฌผ๊ณ ๊ธฐ ๊ฐฏ์ˆ˜
    ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ๋‚จ์•˜๋Š”์ง€ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์ด์ฆˆ๋ณ„๋กœ ๋ฌผ๊ณ ๊ธฐ์˜ ์ˆซ์ž๋ฅผ ์„ธ์–ด๋†“๋Š”๋‹ค.

  3. ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์œ„์น˜
    • ์ด๋™ํ•˜๊ธฐ
      ๋ณด๋“œ์˜ ๊ฐ’์ด ์•„๊ธฐ ์ƒ์–ด์˜ ์‚ฌ์ด์ฆˆ ์ดํ•˜๋ฉด ์ด๋™

    • ๋จน์„ ๋ฌผ๊ณ ๊ธฐ ์œ„์น˜ ์ฒดํฌ
      0์ด ์•„๋‹ˆ๊ณ  ์•„๊ธฐ์ƒ์–ด๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋จน์„ ์ˆ˜ ์žˆ๋‹ค.

    • ๋จน๋Š” ๋ฌผ๊ณ ๊ธฐ ์กฐ๊ฑด ์ฒดํฌ
      ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ์˜ ์•„๊ธฐ์ƒ์–ด๊ฐ€ ์›€์ง์ธ ๊ฑฐ๋ฆฌ, y์ขŒํ‘œ, x์ขŒํ‘œ๋ฅผ heap์— ๋„ฃ๋Š”๋‹ค. heappopํ•˜๋ฉด ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด -> ๊ฐ€์žฅ ์œ„ -> ๊ฐ€์žฅ ์™ผ์ชฝ์ธ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ์˜ ์ขŒํ‘œ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    IndexError๊ฐ€ ๋– ์„œ ์–ด๋ ค์› ๋Š”๋ฐ ์ด๋Š” ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๋ฆฌ๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๊ทธ ์ฃผ์œ„์— ์•„๊ธฐ ์ƒ์–ด๋ณด๋‹ค ํฐ ๋ฌผ๊ณ ๊ธฐ๋“ค ๋–„๋ฌธ์— ์ด๋™ํ•  ์ˆ˜ ์—†์–ด heap์ด ๋น„์–ด์žˆ๋Š” ์ƒํƒœ์ผ ๋•Œ ๋ฐœ์ƒํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ heap์ด ๋น„์–ด์žˆ๋‹ค๋ฉด None์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ฒ˜๋ฆฌํ•˜์˜€๋‹ค.


  4. ํ•ด๋‹น ์œ„์น˜ ๋ฌผ๊ณ ๊ธฐ ๋จน๊ธฐ
    ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์œ„์น˜ ์ฒดํฌํ•ด์„œ None์ด๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ์‹œ์ผฐ๋‹ค.
    ๊ฐ’์ด ์žˆ์œผ๋ฉด ์•„๊ธฐ ์ƒ์–ด๊ฐ€ ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ์‹œ๊ฐ„์ดˆ๋ฅผ ์˜ฌ๋ฆฌ๊ณ  ํ•ด๋‹น ์œ„์น˜์˜ ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์—ˆ์œผ๋‹ˆ ์•„๊ธฐ ์ƒ์–ด๊ฐ€ ๋ฌผ๊ณ ๊ธฐ ๋จน์€ ์ˆ˜๋ฅผ +1ํ•˜๊ณ  board์˜ ํ•ด๋‹น ์œ„์น˜๋ฅผ 0์œผ๋กœ ๋ฐ”๊พผ๋‹ค.

  5. ์•„๊ธฐ์ƒ์–ด ํฌ๊ธฐ ์ฒดํฌ
    ์•„๊ธฐ์ƒ์–ด๊ฐ€ ๋จน์€ ๋ฌผ๊ณ ๊ธฐ ์ˆ˜์™€ ์•„๊ธฐ ์ƒ์–ด ํฌ๊ธฐ๊ฐ€ ๊ฐ™์œผ๋ฉด ์•„๊ธฐ ์ƒ์–ด๊ฐ€ ํฌ๊ธฐ +1ํ•˜๊ณ  ๋จน์€ ์ˆ˜๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค€๋‹ค.
    ์•„๊ธฐ ์ƒ์–ด๊ฐ€ ์ปค์กŒ์œผ๋‹ˆ ์•„๊ธฐ ์ƒ์–ดํฌ๊ธฐ -1 ์‚ฌ์ด์ฆˆ์˜ ๋ฌผ๊ณ ๊ธฐ๋“ค์„ ๋จน์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ๊ฐœ์ˆ˜์— ๊ทธ๋งŒํผ ๋”ํ•ด์ค€๋‹ค. ๋ฌผ๊ณ ๊ธฐ ์‚ฌ์ด์ฆˆ๋Š” 1~6๊นŒ์ง€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•„๊ธฐ ์ƒ์–ด ํฌ๊ธฐ๊ฐ€ 6์ดํ•˜์ผ ๊ฒฝ์šฐ์—๋งŒ ์ด ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•œ๋‹ค.

๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys; input = sys.stdin.readline
from collections import deque
from heapq import heappush, heappop

class Baby:
    def __init__(self) -> None:
        self.y = 0
        self.x = 0
        self.size = 2
        self.eatCnt = 0

    def setLocation(self,y, x):
        self.y = y
        self.x = x

dy = (-1,0,1,0)
dx = (0,1,0,-1)

def findEatable():
    visited = [[False for _ in range(n)] for _ in range(n)]
    queue = deque()
    queue.append((baby.y,baby.x,0))
    visited[baby.y][baby.x] = True
    target = []

    while queue:
        y,x,dist = queue.popleft()
        if 0 < board[y][x] < baby.size:
            heappush(target,(dist,y,x))
            continue
        for i in range(4):
            ny, nx = y + dy[i], x + dx[i]
            if 0 <= ny < n and 0 <= nx < n and not visited[ny][nx]:
                if board[ny][nx] <= baby.size:
                    visited[ny][nx] = True
                    queue.append((ny,nx,dist+1))
    if target: return heappop(target)
    return None

def eatFish(y,x):
    global eatableNum
    board[y][x] = 0
    eatableNum -= 1
    baby.eatCnt += 1
    baby.setLocation(y,x)

def sizeChk():
    global eatableNum
    if baby.eatCnt == baby.size:
        if baby.size <= 6: eatableNum += fishes[baby.size]
        baby.size += 1
        baby.eatCnt = 0

n = int(input())
board = []
baby = Baby()
fishes = dict().fromkeys(range(1,7),0)

for i in range(n):
    board.append(list(map(int,input().split())))
    for j in range(n):
        fishSize = board[i][j]
        if fishSize == 9: 
            baby.setLocation(i,j)
            board[i][j] = 0
        elif 0 < fishSize: 
            fishes[fishSize] += 1

clock = 0
eatableNum = 0
eatableNum += fishes[1]

while eatableNum:
    tmp = findEatable()
    if not tmp: break
    move, targetY, targetX = tmp
    eatFish(targetY,targetX)
    clock += move
    sizeChk()

print(clock)
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.