Coding Test - cpp/BFS
[백준 2583: 영역 구하기] - C++
에드윈H
2021. 2. 4. 16:41
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int map[100][100];
int chk[100][100];
int dir[5][2] = {
{-1,0}
,{0,1}
,{0,0}
,{1,0}
,{0,-1}
};
int main()
{
int n, m;
int k;
cin >> n >> m >> k;
int x1;
int y1;
int x2;
int y2;
for (int i = 0; i < k; i++)
{
cin >> x1 >> y1;
cin >> x2 >> y2;
for (int j = 0; j < x2 - x1; j++)
{
for (int k = 0; k < y2 - y1; k++)
{
map[j + x1][k + y1] = 1;
}
}
}
queue<pair<int, int>> q;
vector<int> result;
for (int index1 = 0; index1 < m; index1++)
{
for (int index2 = 0; index2 < n; index2++)
{
if (map[index1][index2] != 0)
continue;
q.push({ index1,index2 });
int cnt = 0;
while (!q.empty())
{
int curX = q.front().first;
int curY = q.front().second;
q.pop();
for (int i = 0; i < 5; i++)
{
int nextX = curX + dir[i][0];
int nextY = curY + dir[i][1];
if (nextX < 0 || nextY < 0 || n <= nextY || m <= nextX)
{
continue;
}
if (map[nextX][nextY] == 0)
{
map[nextX][nextY] = 1;
q.push({ nextX,nextY });
cnt++;
}
}
}
result.push_back(cnt);
}
}
sort(result.begin(), result.end());
cout << result.size() << endl;
for (auto a : result)
cout << a << " ";
cout << '\n';
return 0;
}