https://www.acmicpc.net/problem/2583
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dy[4] = { 0,-1,0,1 };
int dx[4] = { -1,0,1,0 };
int m, n, k;
int Count;
int visited[101][101];
vector<int> v;
void FloodFill(int x1, int y1,int x2, int y2)
{
for (int i = y1; i < y2; i++)
for (int j = x1; j < x2; j++)
visited[i][j] = true;
}
void DFS(int y, int x)
{
visited[y][x] = true;
Count++;
for (int i = 0; i < 4; i++)
{
int ny = dy[i] + y;
int nx = dx[i] + x;
if(ny >= 0 && ny < m && nx >= 0 && nx < n)
{
if(visited[ny][nx] == false)
{
DFS(ny, nx);
}
}
}
}
int main()
{
cin >> m >> n >> k;
for (int i = 0; i < k; i++)
{
int x1{}, y1{}, x2{}, y2{};
cin >> x1 >> y1 >> x2 >> y2;
FloodFill(x1, y1, x2, y2);
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if(visited[i][j] == false)
{
Count = 0;
DFS(i, j);
v.push_back(Count);
}
}
}
sort(v.begin(), v.end());
cout << v.size() << endl;
for (int data : v)
cout << data << " ";
}
- 4방향으로 움직이는 맵에 FloodFill 알고리즘을 이용해 색을 칠해준다
- 이제 색이 없는 영역의 크기를 구하기 위해 DFS를 돌아주는데 Count 변수를 이용해 각각의 크기를 vector에 저장해준면 되는 문제이다
'코딩 태스트 > 문제풀이' 카테고리의 다른 글
빈도 정렬(x) (0) | 2025.01.10 |
---|---|
사과 담기 게임(x) (0) | 2025.01.10 |
안전 영역(x) (0) | 2025.01.10 |
유기농 배추(x) (0) | 2025.01.10 |
미로 탐색(x) (0) | 2025.01.10 |