-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210125_BOJ_2667.cpp
More file actions
80 lines (63 loc) · 1.2 KB
/
Copy path210125_BOJ_2667.cpp
File metadata and controls
80 lines (63 loc) · 1.2 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
73
74
75
76
77
78
79
80
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int N = 0;
//int m[25][25]{ 0 };
vector<vector<int>> m;
void input() {
cin >> N;
m.assign(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
string t; cin >> t;
for (int j = 0; j < t.size(); ++j) {
m[i][j] = t[j] - '0';
}
}
}
int bfs(int a, int b) {
int numOfHouse = 0;
int dir[4][2]{ {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
queue<pair<int, int>> q;
q.push({ a, b });
m[a][b] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
numOfHouse++;
q.pop();
for (int i = 0; i < 4; ++i) {
int n_x = x + dir[i][0];
int n_y = y + dir[i][1];
if (n_x >= 0 && n_x < N && n_y >= 0 && n_y < N && m[n_x][n_y]) {
q.push({ n_x, n_y });
m[n_x][n_y] = 0;
}
}
}
return numOfHouse;
}
void findAnswer() {
int count = 0;
vector<int> h;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (m[i][j] == 1) {
h.push_back(bfs(i, j));
count++;
}
}
}
cout << count << "\n";
sort(h.begin(), h.end());
for (int i = 0; i < h.size(); ++i) {
cout << h[i] << "\n";
}
}
int main() {
input();
findAnswer();
return 0;
}