#include
#include
#include
using namespace std;
const int MAX = 26;
int n, index = 1, cnt;
string s;
int apart[MAX][MAX];
int answer[MAX];
struct Move {
int x, y;
};
Move mv[4] = { {1,0},{-1,0},{0,1},{0,-1} };
void dfs(int i, int j) {
cnt++;
apart[i][j] = index;
for (int k = 0; k < 4; k++) {
int movei = i + mv[k].x;
int movej = j + mv[k].y;
if (movei >= 0 && movei < n && movej >= 0 && movej < n && apart[movei][movej]==1)
dfs(movei, movej);
}
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s;
for (int j = 0; j < n; j++)
apart[i][j] = s[j] - '0';
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (apart[i][j] == 1) {
index++;
cnt = 0;
dfs(i, j);
answer[index - 2] = cnt;
}
}
}
cout << index-1 << endl;
sort(answer, answer + index-1);
for (int i = 0; i < index-1; i++)
cout << answer[i] << endl;
}
'백준 알고리즘 > BFS' 카테고리의 다른 글
백준 1012번 C++ (0) | 2019.08.08 |
---|---|
백준 7576번 C++ (0) | 2019.08.08 |
백준 1697번 C++ (0) | 2019.08.07 |
백준 2178번 C++ (0) | 2019.08.07 |
백준 7576번 C++ (0) | 2019.02.24 |