백준
백준 소스코드 [C++] 1012 유기농 배추
Hani_Levenshtein
2020. 8. 22. 16:02
https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 �
www.acmicpc.net
백준 소스코드 [C++] 1012 유기농 배추
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
using namespace std;
int x, y,sum;
int arr[52][52];
bool check[52][52];
queue <pair<int, int>> q;
pair<int, int> pp,p[4] = { {1,0},{-1,0},{0,1},{0,-1} };
void bfs() {
while (q.empty() != true) {
pp=q.front();
q.pop();
for (int i = 0;i < 4;i++) {
if (arr[pp.first + p[i].first][pp.second + p[i].second] == 1
&& check[pp.first + p[i].first][pp.second + p[i].second] == true) {
q.push(make_pair(pp.first + p[i].first, pp.second + p[i].second));
check[pp.first + p[i].first][pp.second + p[i].second] = false;
}
}
}
sum++;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
int num,X,Y;
while (t--) {
cin >> x >> y >> num;
sum = 0;
for (int i = 0;i < 52;i++) {
memset(arr[i], 0, sizeof(int) * 52);
memset(check[i], true, sizeof(bool)* 52);
}
for (int i = 0;i < num;i++) {
cin >> X >> Y;
arr[Y+1][X+1] = 1;
}
for (int j = 1;j <=y;j++)
for (int i = 1;i <= x;i++) {
if (arr[j][i] == 1 && check[j][i] ==true) {
q.push(make_pair(j, i));
check[j][i] = false;
bfs();
}
}
cout << sum << '\n';
}
return 0;
}