https://www.acmicpc.net/problem/2583
2583번: 영역 구하기
첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오
www.acmicpc.net
이 문제는 단순한 그래프 탐색이다.
하나 다른 문제들과 다른 점이 있다면, 아마 처음에 직사각형들을 색칠한다는 것?
그 후에는 그냥 단순히 넓이를 하나씩 입력해주고 그거를 그대로 출력해주면 끝난다.
from collections import deque
import sys
import heapq
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
result_list = list()
M, N, K = map(int, sys.stdin.readline().split())
paper = [[0 for _ in range(M)] for _ in range(N)]
for _ in range(K):
start_x, start_y, end_x, end_y = map(int, sys.stdin.readline().split())
for x in range(start_x, end_x):
for y in range(start_y, end_y):
paper[x][y] = 1
for x in range(N):
for y in range(M):
if paper[x][y] == 0:
need_visit = deque()
paper[x][y] = -1
need_visit.append([x, y])
result = 0
while need_visit:
cur_x, cur_y = need_visit.popleft()
result += 1
for i in range(4):
next_x, next_y = cur_x + dx[i], cur_y + dy[i]
if 0 <= next_x < N and 0 <= next_y < M and paper[next_x][next_y] == 0:
paper[next_x][next_y] = -1
need_visit.append([next_x, next_y])
heapq.heappush(result_list, result)
print(len(result_list))
while result_list:
print(heapq.heappop(result_list), end=' ')
'알고리즘 > 그래프' 카테고리의 다른 글
백준 1976 여행 가자 (Python) (0) | 2024.02.18 |
---|---|
백준 1967 트리의 지름 (Python) (0) | 2024.02.18 |
백준 16234 인구 이동 (Python) (0) | 2024.02.18 |
백준 5014 스타트링크 (Python) (1) | 2024.02.18 |
백준 1167 트리의 지름 (Python) (0) | 2024.02.18 |