-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2583.cpp
More file actions
59 lines (57 loc) · 1.3 KB
/
2583.cpp
File metadata and controls
59 lines (57 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int M, N, K, component = 0;
int map[101][101];
int visited[101][101];
int dfs(int i, int j, int c)
{
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int nx, ny;
for (int k = 0; k < 4; ++k)
{
nx = i + d[k][0];
ny = j + d[k][1];
if (0 <= nx && nx < M && 0 <= ny && ny < N && !map[nx][ny] && !visited[nx][ny])
{
visited[nx][ny] = true;
c += dfs(nx, ny, 0) + 1;
}
}
return c;
}
int main()
{
cin >> M >> N >> K;
int left, right, top, bottom;
vector<int> area;
for (int k = 0; k < K; ++k)
{
cin >> left >> top >> right >> bottom;
for (int i = top; i < bottom; ++i)
{
for (int j = left; j < right; ++j)
{
map[i][j] = 1;
}
}
}
for (int i = 0; i < M; ++i)
{
for (int j = 0; j < N; ++j)
{
if (!map[i][j] && !visited[i][j])
{
visited[i][j] = true;
component++;
area.push_back(dfs(i, j, 1));
}
}
}
sort(area.begin(), area.end());
cout << component<< '\n';
for(int i=0;i<area.size();++i){
cout << area[i] << " ";
}
}