-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon2667.cpp
More file actions
72 lines (65 loc) · 1.28 KB
/
BaekJoon2667.cpp
File metadata and controls
72 lines (65 loc) · 1.28 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
60
61
62
63
64
65
66
67
68
69
70
71
72
//BaekJoon 2667 단지번호 붙이기
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <utility>
#include <list>
using namespace std;
int n;
int arr[25][25];
bool visited[25][25] = { false, };
list<int> l;
int num = 0;
int numApt;
pair<int, int> cur;
queue<pair<int, int>> qu;
int isNear(int a, int b){
if (a < 0 || a >= n || b < 0 || b >= n) {
return 0;
}
if (arr[a][b] == 1 && visited[a][b] == false) {
qu.push(make_pair(a, b));
visited[a][b] = true;
return 1;
}
return 0;
}
int main(void)
{
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%1d", &arr[i][j]);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == 1 && visited[i][j] == false) {
qu.push(make_pair(i, j));
visited[i][j] = true;
num++;
numApt = 1;
while (!qu.empty()) {
cur = qu.front();
qu.pop();
int a = cur.first;
int b = cur.second;
numApt += isNear(a + 1, b);
numApt += isNear(a, b - 1);
numApt += isNear(a, b + 1);
numApt += isNear(a - 1, b);
}
l.push_back(numApt);
}
}
}
l.sort();
cout << num << "\n";
list<int>::iterator iter;
for (iter = l.begin(); iter != l.end(); ++iter) {
cout << *iter << "\n";
}
return 0;
}