VioletaBabel
1012번: 유기농 배추 본문
#include<cstdio>
#include<algorithm>
void deleteComponent(int x, int y, int m, int n);
bool sejong[50][50];
int count;
int main()
{
int t, m, n, k;
for (scanf("%d", &t); t--;)
{
count = 0;
std::fill_n(&sejong[0][0], 2500, 0);
scanf("%d %d %d", &m, &n, &k);
for (int i = 0, x, y; i < k; ++i)
{
scanf("%d %d", &x, &y);
sejong[y][x] = 1;
}
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (sejong[i][j] == 1)
{
deleteComponent(j, i, m, n);
++count;
}
printf("%d\n", count);
}
}
void deleteComponent(int x, int y, int m, int n)
{
sejong[y][x] = 0;
if (y > 0)
if (sejong[y - 1][x] == 1)
deleteComponent(x, y - 1, m, n);
if (y < n - 1)
if (sejong[y + 1][x] == 1)
deleteComponent(x, y + 1, m, n);
if (x > 0)
if (sejong[y][x - 1] == 1)
deleteComponent(x - 1, y, m, n);
if (x < m - 1)
if (sejong[y][x + 1] == 1)
deleteComponent(x + 1, y, m, n);
}
'백준 > 백준-C++' 카테고리의 다른 글
| 9507번: Generations of Tribbles (0) | 2017.06.04 |
|---|---|
| 9466번: 텀 프로젝트 (0) | 2017.06.04 |
| 1629번: 곱셈 (0) | 2017.06.03 |
| 1463번: 1로 만들기 (0) | 2017.06.03 |
| 11052번: 붕어빵 판매하기 (0) | 2017.06.01 |