BOJ - 16294 - Bee Problem

Updated:

import sys
from collections import deque

dx = [0, 0, 1, 1, -1, -1]
dy = [2, -2, 1, -1, 1, -1]


def BFS(bee, check, N, M, x, y):
    dq = deque()
    dq.append((x, y))
    check[x][y] = True

    count = 0
    while dq:
        count += 1
        x, y = dq.popleft()


        for k in range(6):
            nx, ny = x + dx[k], y + dy[k]

            if 0 <= nx < N and 0 <= ny < M:
                if check[nx][ny] is False and bee[nx][ny] == '.':
                    dq.append((nx, ny))
                    check[nx][ny] = True

    return count

def solution():
    H, N, M = map(int, sys.stdin.readline().split())
    bee = []

    for i in range(N):
        if i % 2 == 0:
            bee.append(list(sys.stdin.readline()))
            bee[i][-1] = ''
        else: bee.append(list(sys.stdin.readline().rstrip()))

    check = [[False for _ in range(M * 2)] for _ in range(N)]

    honey = []
    for i in range(N):
        for j in range(M * 2):
            if bee[i][j] == '.' and check[i][j] is False:
                honey.append(BFS(bee, check, N, M*2, i, j))

    honey.sort(reverse=True)
    answer = 0
    idx = 0
    while H > 0:
        answer += 1
        H -= honey[idx]
        idx += 1

    print(answer)
solution()

https://www.acmicpc.net/problem/16294

Categories:

Updated:

Leave a comment