![[백준 1012번 / JAVA] 유기농 배추](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FAESmQ%2FbtsldOSu5cY%2FFD0R1U0pEU9uTfkZIsr3A1%2Fimg.png)
[백준 1012번 / JAVA] 유기농 배추P.S./백준2023. 6. 25. 13:38
Table of Contents
728x90
728x90
문제 링크
https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
풀이
자신있는 DFS로 풀었고, BFS는 몇 번 해보지 않아 사용을 해보기로 했다.
DFS
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BakJoon1012 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[][] cabbage = new int[M][N];
boolean[][] bl = new boolean[M][N];
for (int j = 0; j < K; j++) {
st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
cabbage[x][y] = 1;
}
int result = 0;
for(int j = 0; j < cabbage.length; j++){
for(int k = 0; k < cabbage[j].length; k++){
if(cabbage[j][k] == 1 && !bl[j][k]) {
dfs(cabbage, bl, j, k);
result++;
}
}
}
System.out.println(result);
}
br.close();
}
private static void dfs(int[][] cabbage, boolean[][] bl, int x, int y) {
if(x < 0 || x >= bl.length || y < 0 || y >= bl[0].length) return;
if(bl[x][y]) return;
bl[x][y] = true;
if(cabbage[x][y] == 1){
dfs(cabbage, bl, x+1, y);
dfs(cabbage, bl, x-1, y);
dfs(cabbage, bl, x, y+1);
dfs(cabbage, bl, x, y-1);
}
}
}
BFS
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BakJoon1012 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int M = Integer.parseInt(st.nextToken());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[][] cabbage = new int[M][N];
boolean[][] bl = new boolean[M][N];
for (int j = 0; j < K; j++) {
st = new StringTokenizer(br.readLine(), " ");
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
cabbage[x][y] = 1;
}
int result = 0;
for(int j = 0; j < cabbage.length; j++){
for(int k = 0; k < cabbage[j].length; k++){
if(cabbage[j][k] == 1 && !bl[j][k]) {
bfs(cabbage, bl, j, k);
result++;
}
}
}
System.out.println(result);
}
br.close();
}
private static void bfs(int[][] cabbage, boolean[][] bl, int x, int y) {
Queue<int []> queue = new LinkedList<>();
queue.offer(new int[]{x, y});
bl[x][y] = true;
int[] X = new int[]{-1, 1, 0, 0};
int[] Y = new int[]{0, 0, -1, 1};
while(!queue.isEmpty()){
int[] poll = queue.poll();
for(int i=0; i<4; i++){
int xx = poll[0] + X[i];
int yy = poll[1] + Y[i];
if(xx < 0 || xx >= bl.length || yy < 0 || yy >= bl[0].length) continue;
if(cabbage[xx][yy] == 1 && !bl[xx][yy]){
queue.offer(new int[]{xx, yy});
bl[xx][yy] = true;
}
}
}
}
}
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!