BOJ - 2573 - 빙산

Updated:

import sys
from collections import deque

def BFS(i, j, visit, data, N, M):
    q = deque()
    visit[i][j] = True
    retdq = deque()

    q.append([i, j])
    while q:
        x, y = q.popleft()
        seaCnt = 0

        for nx, ny in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            dx, dy = nx + x, ny + y
            if 0 <= dx < N and 0 <= dy < M and not visit[dx][dy]:
                if data[dx][dy]:
                    visit[dx][dy] = True
                    q.append([dx, dy])
                else: seaCnt += 1

        if seaCnt:
            retdq.append([x, y, seaCnt])
    return retdq

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

    data = []
    year = 0

    for _ in range(N):
        data.append(list(map(int, input().split(' '))))

    year = 0
    while True:
        iceCnt = 0
        visit = [[False for _ in range(M)] for _ in range(N)]
        for i in range(N):
            for j in range(M):
                if not visit[i][j] and data[i][j]:
                    iceCnt += 1
                    iceberg = BFS(i, j, visit, data, N, M)
                    while iceberg:
                        x, y, cnt = iceberg.popleft()
                        data[x][y] = max(data[x][y] - cnt, 0)

        if iceCnt == 0:
            year = 0
            break
        if iceCnt >= 2:
            break
        year += 1



    print(year)

solution()

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

Categories:

Updated:

Leave a comment