-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2667.cpp
More file actions
70 lines (59 loc) · 1.08 KB
/
2667.cpp
File metadata and controls
70 lines (59 loc) · 1.08 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
// ref : https://sw-ko.tistory.com/88
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, cnt;
int arr[26][26];
bool visited[26][26] = { false, };
int dx[4] = { -1, 1,0,0};
int dy[4] = {0,0,-1,1};
vector<int> ans;
void dfs(int x, int y);
int main() {
// input
cin >> n;
string line;
for (int i = 0; i < n; i++) {
cin >> line;
for (int j = 0; j < n; j++) {
arr[i][j] = line[j] - 48;
}
}
// dfs
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] == 1 && visited[i][j] == false) {
cnt = 0;
dfs(i, j);
ans.push_back(cnt);
}
}
}
// result
sort(ans.begin(), ans.end());
cout << ans.size() << endl;
if (ans.size() == 0) {
cout << 0;
}
else {
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
}
return 0;
}
void dfs(int x, int y) {
cnt++;
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx > n || ny < 0 || ny > n) {
continue;
}
if (arr[nx][ny] == 1 && visited[nx][ny] == false) {
dfs(nx, ny);
}
}
}