-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.cpp
59 lines (56 loc) · 1.43 KB
/
main.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
// Authored by : seastar105
// Co-authored by : -
// Link : http://boj.kr/64816e87e7c24254ae15748732036df3
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int N;
string board[30];
bool vis[30][30];
bool valid(int x, int y) {
return 0 <= x && x < N && 0 <= y && y < N;
}
int bfs(int src_x, int src_y) {
queue<pair<int,int>> q;
vis[src_x][src_y] = true;
q.push({src_x, src_y});
int ret = 0;
while(!q.empty()) {
pair<int,int> p = q.front();
int x = p.first;
int y = p.second;
++ret;
q.pop();
for(int i=0;i<4;++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if(!valid(nx, ny)) continue;
if(vis[nx][ny]) continue;
if(board[nx][ny] == '0') continue;
vis[nx][ny] = true;
q.push({nx, ny});
}
}
return ret;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; ++i) cin >> board[i];
vector<int> ans;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (vis[i][j]) continue;
if (board[i][j] == '0') continue;
// BFS start
int cnt = bfs(i, j);
ans.push_back(cnt);
}
}
sort(ans.begin(), ans.end());
cout << ans.size() << '\n';
for (int x : ans) cout << x << '\n';
return 0;
}