개발자 뺚
[BAEKJOON ONLINE JUDGE] 1012번 : 유기농 배추 본문
시간 제한 : 1 초
메모리 제한 : 512 MB
문제
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 효과적인 배추흰지렁이를 구입하기로 결심한다. 이 지렁이는 배추근처에 서식하며 해충을 잡아 먹음으로써 배추를 보호한다. 특히, 어떤 배추에 배추흰지렁이가 한 마리라도 살고 있으면 이 지렁이는 인접한 다른 배추로 이동할 수 있어, 그 배추들 역시 해충으로부터 보호받을 수 있다. 한 배추의 상하좌우 네 방향에 다른 배추가 위치한 경우에 서로 인접해있는 것이다.
한나가 배추를 재배하는 땅은 고르지 못해서 배추를 군데군데 심어 놓았다. 배추들이 모여있는 곳에는 배추흰지렁이가 한 마리만 있으면 되므로 서로 인접해있는 배추들이 몇 군데에 퍼져있는지 조사하면 총 몇 마리의 지렁이가 필요한지 알 수 있다. 예를 들어 배추밭이 아래와 같이 구성되어 있으면 최소 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 |
입력
입력의 첫 줄에는 테스트 케이스의 개수 T가 주어진다. 그 다음 줄부터 각각의 테스트 케이스에 대해 첫째 줄에는 배추를 심은 배추밭의 가로길이 M(1 ≤ M ≤ 50)과 세로길이 N(1 ≤ N ≤ 50), 그리고 배추가 심어져 있는 위치의 개수 K(1 ≤ K ≤ 2500)이 주어진다. 그 다음 K줄에는 배추의 위치 X(0 ≤ X ≤ M-1), Y(0 ≤ Y ≤ N-1)가 주어진다. 두 배추의 위치가 같은 경우는 없다.
출력
각 테스트 케이스에 대해 필요한 최소의 배추흰지렁이 마리 수를 출력한다.
#include<stdio.h>
#include<stdlib.h>
typedef struct Queue {
int row;
int col;
struct Queue* link;
} Queue;
typedef struct {
Queue* front;
Queue* rear;
} QueueType;
void queue_init(QueueType* q) {
q->front = q->rear = NULL;
}
int is_queue_empty(QueueType* q) {
return q->front == NULL;
}
void enqueue(QueueType* node, int row, int col) {
Queue* tmp = (Queue*)malloc(sizeof(Queue));
tmp->row = row;
tmp->col = col;
tmp->link = NULL;
if (is_queue_empty(node)) {
node->front = tmp;
node->rear = tmp;
}
else {
node->rear->link = tmp;
node->rear = tmp;
}
}
Queue* dequeue(QueueType* node) {
Queue* tmp = node->front;
node->front = node->front->link;
if (node->front == NULL)
node->rear = NULL;
return tmp;
}
int main() {
int n = 0;
scanf(" %d", &n);
for (int i = 0; i < n; i++) {
int row_len = 0;
int col_len = 0;
int ceb_cnt = 0;
scanf(" %d %d %d", &col_len, &row_len, &ceb_cnt);
int** arr = (int**)malloc(sizeof(int*) * row_len);
for (int j = 0; j < row_len; j++)
arr[j] = (int*)malloc(sizeof(int) * col_len);
for (int j = 0; j < row_len; j++)
for (int k = 0; k < col_len; k++)
arr[j][k] = 0;
for (int j = 0; j < ceb_cnt; j++) {
int ceb_row = 0;
int ceb_col = 0;
scanf(" %d %d", &ceb_col, &ceb_row);
arr[ceb_row][ceb_col] = 1;
}
QueueType* head = (QueueType*)malloc(sizeof(QueueType));
queue_init(head);
int count = 0;
for (int j = 0; j < row_len; j++)
for (int k = 0; k < col_len; k++)
if (arr[j][k] == 1) {
count++;
enqueue(head, j, k);
arr[j][k] = 0;
while (!is_queue_empty(head)) {
Queue* tmp = dequeue(head);
if (tmp->row > 0)
if (arr[tmp->row - 1][tmp->col] == 1) {
enqueue(head, tmp->row - 1, tmp->col);
arr[tmp->row - 1][tmp->col] = 0;
}
if (tmp->row < row_len - 1)
if (arr[tmp->row + 1][tmp->col] == 1) {
enqueue(head, tmp->row + 1, tmp->col);
arr[tmp->row + 1][tmp->col] = 0;
}
if (tmp->col > 0)
if (arr[tmp->row][tmp->col - 1] == 1) {
enqueue(head, tmp->row, tmp->col - 1);
arr[tmp->row][tmp->col - 1] = 0;
}
if (tmp->col < col_len - 1)
if (arr[tmp->row][tmp->col + 1] == 1) {
enqueue(head, tmp->row, tmp->col + 1);
arr[tmp->row][tmp->col + 1] = 0;
}
}
}
printf("%d\n", count);
free(arr);
}
return 0;
}
'Solution > C' 카테고리의 다른 글
[BAEKJOON ONLINE JUDGE] 2668번 : 숫자고르기 (1) | 2024.01.22 |
---|---|
[BAEKJOON ONLINE JUDGE] 2667번 : 단지번호붙이기 (2) | 2024.01.21 |
[BAEKJOON ONLINE JUDGE] 2563번 : 색종이 (0) | 2024.01.18 |
[BAEKJOON ONLINE JUDGE] 10798번 : 세로읽기 (1) | 2024.01.15 |
[BAEKJOON ONLINE JUDGE] 9086번 : 문자열 (0) | 2023.11.28 |