VioletaBabel
2667번: 단지번호붙이기 본문
#include<cstdio>
#include<algorithm>
void apart(int x, int y, int n);
char xi[25][26];
int num[313];
int apartCount;
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%s", &xi[i]);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if (xi[i][j] == '1')
{
apart(j, i, n);
++apartCount;
}
std::sort(num, num + apartCount);
printf("%d\n", apartCount);
for (int i = 0; i < apartCount; ++i)
printf("%d\n", num[i]);
}
void apart(int x, int y, int n)
{
xi[y][x] = '0';
++num[apartCount];
if (y > 0)
if (xi[y - 1][x] == '1')
apart(x, y - 1, n);
if (y < n - 1)
if (xi[y + 1][x] == '1')
apart(x, y + 1, n);
if (x > 0)
if (xi[y][x - 1] == '1')
apart(x - 1, y, n);
if (x < n - 1)
if (xi[y][x + 1] == '1')
apart(x + 1, y, n);
}
'백준 > 백준-C++' 카테고리의 다른 글
7576번: 토마토 (0) | 2017.06.09 |
---|---|
2178번: 미로 탐색 (0) | 2017.06.09 |
1260번: DFS와 BFS (0) | 2017.06.09 |
2156번: 포도주 시식 (실패) (0) | 2017.06.07 |
2193번: 이친수 (0) | 2017.06.06 |