-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_dfs_stack.cpp
65 lines (54 loc) · 1.48 KB
/
04_dfs_stack.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
// 백준 2667번
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int cnt;
int arr[25][25];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
vector<pair<int,int>> v;
vector<int> answer;
int main() {
scanf("%d",&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) {
cnt=0;
arr[i][j]=0;
v.push_back(make_pair(i,j));
while(!v.empty()) {
cnt++;
pair<int,int> now = v.back();
v.pop_back();
for (int k=0; k<4; k++) {
int nx = now.first + dx[k];
int ny = now.second + dy[k];
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
if (arr[nx][ny]==1) {
arr[nx][ny]=0;
v.push_back(make_pair(nx,ny));
}
}
}
}
answer.push_back(cnt);
}
}
}
sort(answer.begin(), answer.end());
int size = answer.size();
printf("%d\n", size);
for (auto iter=answer.begin(); iter != answer.end(); ++iter) {
printf("%d\n", *iter);
}
return 0;
}