반응형
🚀 문제
🚀 접근 방법
DFS로 풀었다.
단지번호붙이기와 비슷한 문제이다.
🚀 코드
import java.io.*;
import java.util.*;
public class Main {
public static int[] dx = new int[] {-1, 0, 1, 0};
public static int[] dy = new int[] {0, 1, 0, -1};
public static int count = 0;
public static boolean[][] map;
public static int n, m, k;
public static void dfs(int x, int y){
for(int i=0; i<4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=0 && ny>=0 && nx<n && ny<m && map[nx][ny]){
map[nx][ny] = false;
dfs(nx, ny);
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(in.readLine());
for(int iter=0; iter<t; iter++){
int[] infos = Arrays.stream(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
n = infos[0];
m = infos[1];
k = infos[2];
map = new boolean[n][m];
for(int j=0; j<k; j++){
int[] p = Arrays.stream(in.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
map[p[0]][p[1]] = true;
}
for(int i = 0; i<n; i++){
for(int j=0; j<m; j++){
if(map[i][j]){
dfs(i, j);
count++;
}
}
}
out.write(count + "\n");
count = 0;
}
out.flush();
out.close();
in.close();
}
}
반응형
'코딩테스트' 카테고리의 다른 글
[백준-2606번] 바이러스 풀이 - Java (0) | 2025.06.03 |
---|---|
[백준-2178번] 미로 탐색 풀이 - Java (0) | 2025.06.03 |
[백준-1260번] DFS와 BFS 풀이 - Java (0) | 2025.06.03 |
[자료구조] 덱(Deque, Double-ended queue) (0) | 2025.06.03 |
[자료구조] 힙(Heap) - 최소 힙(Min Heap), 최대 힙(Max Heap) (0) | 2025.06.03 |