차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 효과적인 배추흰지렁이를 구입하기로 결심한다. 이 지렁이는 배추근처에 서식하며 해충을 잡아 먹음으로써 배추를 보호한다. 특히, 어떤 배추에 배추흰지렁이가 한 마리라도 살고 있으면 이 지렁이는 인접한 다른 배추로 이동할 수 있어, 그 배추들 역시 해충으로부터 보호받을 수 있다. 한 배추의 상하좌우 네 방향에 다른 배추가 위치한 경우에 서로 인접해있는 것이다.
한나가 배추를 재배하는 땅은 고르지 못해서 배추를 군데군데 심어 놓았다. 배추들이 모여있는 곳에는 배추흰지렁이가 한 마리만 있으면 되므로 서로 인접해있는 배추들이 몇 군데에 퍼져있는지 조사하면 총 몇 마리의 지렁이가 필요한지 알 수 있다. 예를 들어 배추밭이 아래와 같이 구성되어 있으면 최소 5마리의 배추흰지렁이가 필요하다. 0은 배추가 심어져 있지 않은 땅이고, 1은 배추가 심어져 있는 땅을 나타낸다.
1 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 1 1 0 0 0 1 1 1
0 0 0 0 1 0 0 1 1 1
그래프 탐색 문제 -> BFS DFS 중 하나로 해결 가능
public class Main {
static int[][] map;
static int n;
static int m;
static int c;
static int[][] visit;
static int[] count;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int r = scanner.nextInt();
count = new int[r];
for (int i = 0; i < r; i++) {
n = scanner.nextInt();
m = scanner.nextInt();
c = scanner.nextInt();
map = new int[n][m];
visit = new int[n][m];
int[] x = new int[c];
int[] y = new int[c];
for (int j = 0; j < c; j++) {
x[j] = scanner.nextInt();
y[j] = scanner.nextInt();
map[x[j]][y[j]] = 1;
}
for (int j = 0; j < c; j++) {
if (visit[x[j]][y[j]] == 0) {
visit[x[j]][y[j]] = 1;
BFS(x[j],y[j]);
count[i]++;
}
}
}
for (int i=0; i < r; i++) {
System.out.println(count[i]);
}
}
public static void BFS(int x, int y){
Queue<Integer> queue = new ArrayDeque<>(n*m);
queue.add(x);
queue.add(y);
while(!queue.isEmpty()){
x = queue.poll();
y = queue.poll();
//위
if (x > 0){
if (map[x-1][y] == 1 && visit[x-1][y] == 0){
queue.add(x-1);
queue.add(y);
visit[x - 1][y] = 1;
}
}
//아래
if (x < n-1) {
if (map[x+1][y] == 1 && visit[x+1][y] == 0){
queue.add(x+1);
queue.add(y);
visit[x + 1][y] = 1;
}
}
//왼쪽
if (y > 0){
if (map[x][y-1] == 1 && visit[x][y-1] == 0) {
queue.add(x);
queue.add(y - 1);
visit[x][y - 1] = 1;
}
}
//오른쪽
if (y < m-1) {
if (map[x][y+1] == 1 && visit[x][y+1] == 0) {
queue.add(x);
queue.add(y + 1);
visit[x][y + 1] = 1;
}
}
}
}
}