BackJoon Algorithm - Java
문제
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Back_1012 {
static int M, N, K;
static int[][] cabbage;
static boolean[][] visited;
static int count;
static int[] dx = {0, -1, 0, 1};
static int[] dy = {1, 0, -1, 0};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
count = 0;
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
M = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
cabbage = new int[M][N];
visited = new boolean[M][N];
K = Integer.parseInt(st.nextToken());
for (int j = 0; j < K; j++) {
st = new StringTokenizer(br.readLine(), " ");
int p1 = Integer.parseInt(st.nextToken());
int p2 = Integer.parseInt(st.nextToken());
cabbage[p1][p2] = 1;
}
for (int x = 0; x < M; x++) {
for (int y = 0; y < N; y++) {
if (cabbage[x][y] == 1 && !visited[x][y]) {
dfs(x, y);
count++;
}
}
}
sb.append(count).append("\n");
}
System.out.println(sb);
br.close();
}
static void dfs(int x, int y) {
visited[x][y] = true;
for (int i = 0; i < 4; i++) {
int cx = x + dx[i];
int cy = y + dy[i];
if (cx >= 0 && cy >= 0 && cx < M && cy < N) {
if (!visited[cx][cy] && cabbage[cx][cy] == 1) {
dfs(cx, cy);
}
}
}
}
}
댓글남기기