-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2667.cpp
More file actions
82 lines (71 loc) · 1.52 KB
/
2667.cpp
File metadata and controls
82 lines (71 loc) · 1.52 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
81
82
/**
* @file 2667.cpp
* @brief 단지번호붙이기
* @author Sam Kim (samkim2626@gmail.com)
*
* DFS
* 음료수 얼려먹기 문제랑 비슷
*
* 백준 테스트 케이스 맞왜틀? -> 입출력 주석제거해서 통과
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int n, cnt;
int map[26][26];
bool visited[26][26] = {
false,
};
vector<int> cnts;
pair<int, int> dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void dfs(int x, int y)
{
cnt++;
visited[x][y] = true;
for (int i = 0; i < 4; i++)
{
int newX = x + dir[i].first;
int newY = y + dir[i].second;
if (newX >= 1 && newX <= n && newY >= 1 && newY <= n)
{
if (!visited[newX][newY] && map[newX][newY] == 1)
{
dfs(newX, newY);
}
}
}
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%1d", &map[i][j]);
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (!visited[i][j] && map[i][j] == 1)
{
cnt = 0;
dfs(i, j);
cnts.push_back(cnt);
}
}
}
sort(cnts.begin(), cnts.end());
cout << cnts.size() << '\n';
for (int i = 0; i < cnts.size(); i++)
{
cout << cnts[i] << '\n';
}
return 0;
}