-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2667.cpp
More file actions
74 lines (56 loc) · 1.63 KB
/
2667.cpp
File metadata and controls
74 lines (56 loc) · 1.63 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int main(){
int n;
cin >> n;
int arr[n+1][n+1];
int visited[n+1][n+1];
for(int i=0; i<n; i++){
string str;
cin >> str;
for(int j=0; j<n; j++){
arr[i][j] = str[j]-'0';
visited[i][j] = 0;
}
}
priority_queue<int, vector<int>, greater<int> > result;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(arr[i][j] && !visited[i][j] ){
int sum = 1;
queue<pair<int,int> > q;
q.push(make_pair(i,j));
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
visited[x][y] = true;
for(int k=0; k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
if(nx>=0 && ny>=0 && nx<n && ny<n
&& arr[nx][ny]
&& !visited[nx][ny]){
visited[nx][ny] = true;
sum++;
q.push(make_pair(nx,ny));
}
}
}
result.push(sum);
}
}
}
cout << result.size() <<'\n';
while(!result.empty()){
int a = result.top();
result.pop();
cout << a <<'\n';
}
}