20250719 baekjoon
This commit is contained in:
parent
0eb0f29c16
commit
2dadff05f2
46
code_study/Baekjoon/java/_1012.java
Normal file
46
code_study/Baekjoon/java/_1012.java
Normal file
@ -0,0 +1,46 @@
|
||||
import java.util.*;
|
||||
|
||||
public class _1012 {
|
||||
|
||||
public static boolean dfs(int x, int y, int m, int n, int[][] cabbage, int[][] visited){
|
||||
if (visited[x][y] == 1) return false;
|
||||
visited[x][y] = 1;
|
||||
int[] dx = {1,-1,0,0};
|
||||
int[] dy = {0,0,-1,1};
|
||||
for(int i=0; i<4; i++) {
|
||||
int next_x = x + dx[i];
|
||||
int next_y = y + dy[i];
|
||||
if((next_x>=0)&&(next_y>=0)&&(next_x<m)&&(next_y<n)&&(cabbage[next_x][next_y]==1))
|
||||
dfs(next_x, next_y, m, n, cabbage, visited);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public static void main(String[] args){
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int T = sc.nextInt();
|
||||
for(int t=0; t<T; t++) {
|
||||
int M = sc.nextInt();
|
||||
int N = sc.nextInt();
|
||||
int K = sc.nextInt();
|
||||
int[][] cabbage = new int[M][N];
|
||||
int[][] visited = new int[M][N];
|
||||
for(int k=0; k<K; k++){
|
||||
int m = sc.nextInt();
|
||||
int n = sc.nextInt();
|
||||
cabbage[m][n] = 1;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for(int i=0; i<M; i++) {
|
||||
for(int j=0; j<N; j++) {
|
||||
if(cabbage[i][j] == 1 && visited[i][j] == 0) {
|
||||
if(dfs(i,j,M,N,cabbage,visited)) count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(count);
|
||||
}
|
||||
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user