-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2667.cpp
74 lines (65 loc) · 1.06 KB
/
2667.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 2667. 단지번호 붙이기
// 2019.05.20
// BFS, DFS
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int arr[26][26]; // 단지
bool visit[26][26]; // 방문 유무
// 상, 하, 좌, 우
int dx[4] = { 0,0,-1,1 };
int dy[4] = { 1,-1,0,0 };
int n;
int DFS(int x, int y)
{
int cnt = 1;
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (yy < 0 || yy >= n || xx < 0 || xx >= n)
{
continue;
}
if (arr[xx][yy] && !visit[xx][yy])
{
visit[xx][yy] = 1;
cnt += DFS(xx, yy);
}
}
return cnt;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int j = 0; j < n; j++)
{
arr[i][j] = s[j] - '0';
}
}
vector<int> area;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (!visit[i][j] && arr[i][j] == 1)
{
visit[i][j] = 1;
area.push_back(DFS(i, j));
}
}
}
sort(area.begin(), area.end());
cout << area.size() << endl;
for (int i = 0; i < area.size(); i++)
{
cout << area[i] << endl;
}
return 0;
}