BOJ - 2583 - 영역 구하기

Updated:

import sys
from collections import deque

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

def BFS(Field, N, M, r, c):
    Q = deque()
    Q.append((r, c))
    Field[r][c] = 2
    size = 1

    while Q:
        x, y = Q.popleft()
        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < N and 0 <= ny < M and Field[nx][ny] == 0:
                Q.append((nx, ny))
                Field[nx][ny] = 2
                size += 1
    return 1, size

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

    Field = [[0] * M for _ in range(N)]

    for _ in range(K):
        x1, y1, x2, y2 = map(int, sys.stdin.readline().split())

        for i in range(y1, y2):
            for j in range(x1, x2):
                Field[i][j] = 1

    ret = []

    for i in range(N):
        for j in range(M):
            if Field[i][j] == 0:
                ret.append(BFS(Field, N, M, i, j))
                
    ret.sort()
    print(len(ret))
    for k, v in ret:
        print(v, end=' ')
solution()

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

Categories:

Updated:

Leave a comment