[백준] 2583 영역 구하기
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int N,M;
int board[101][101] = { 0 };
int visited[101][101] = { 0 };
bool inRange(int r, int c) {
if (r < 0 || r >= N) return false;
if (c < 0 || c >= M) return false;
return true;
}
int dirR[4] = { 0, 0, 1, -1 };
int dirC[4] = { 1, -1, 0, 0 };
int bfs(int startR, int startC) {
queue<pair<int, int>> q;
q.push({ startR, startC });
int size = 0;
while (!q.empty()) {
int curR = q.front().first;
int curC = q.front().second;
q.pop();
if (visited[curR][curC]) continue;
visited[curR][curC] = 1;
size++;
for (int d = 0; d < 4; ++d) {
int nextR = curR + dirR[d];
int nextC = curC + dirC[d];
if (!inRange(nextR, nextC)) continue;
if (visited[nextR][nextC]) continue;
if (board[nextR][nextC] == 1) continue;
q.push({ nextR, nextC });
}
}
return size;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int K;
cin >> M >> N >> K;
for (int i = 0; i < K; ++i) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
for (int r = x1; r < x2; ++r) {
for (int c = y1; c < y2; ++c) {
board[r][c] = 1;
}
}
}
int cnt = 0;
vector<int> sizes;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if ((board[i][j] == 0) && (!visited[i][j])) {
sizes.push_back(bfs(i, j));
cnt++;
}
}
}
cout << cnt <<"\n";
sort(sizes.begin(), sizes.end());
for (int i = 0; i < sizes.size(); ++i) {
cout << sizes[i] << " ";
}
return 0;
}