https://www.acmicpc.net/problem/2583
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int m, n, k;
int MAP[101][101];
int cnt = 0;
struct Node {
int row;
int col;
};
int bfs(int row, int col) {
int dr[] = { 0, 0, 1, -1 };
int dc[] = { 1, -1, 0, 0 };
int total = 1;
queue<Node> q;
q.push({ row, col });
while (!q.empty()) {
Node now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextRow = now.row + dr[i];
int nextCol = now.col + dc[i];
if (nextRow < 0 || nextCol < 0 || nextRow >= m || nextCol >= n) continue;
if (MAP[nextRow][nextCol] != 0) continue;
total += 1;
MAP[nextRow][nextCol] = -1;
q.push({ nextRow, nextCol });
}
}
return total;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> m >> n >> k;
for (int i = 0; i < k; i++) {
int sx, ex, sy, ey;
cin >> sy >> sx >> ey >> ex;
for (int x = sx; x < ex; x++) {
for (int y = sy; y < ey; y++) {
MAP[x][y] += 1;
}
}
}
vector<int> v;
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
if (MAP[x][y] == 0) {
MAP[x][y] = -1; // check
cnt += 1;
int base = bfs(x, y);
v.push_back(base);
}
}
}
sort(v.begin(), v.end());
cout << cnt << '\n';
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return 0;
}
왼쪽 아래가 (0,0)일 때 어떻게 풀어야 좋을지 모르겠다. 그냥 뒤집어서 풀어도 맞을까,,,
작가님 느낀 점이 수정이 안 된 것 같은데요..? 확인 부탁드려요