|
1 | 1 | package Week2.개인.유기농배추;
|
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
3 | 8 | public class suhyun {
|
| 9 | + |
| 10 | + static int t, m, n, k, a, b; |
| 11 | + static StringTokenizer st; |
| 12 | + static int[][] graph; |
| 13 | + static int[][] dir = {{0,1}, {1,0}, {0,-1}, {-1,0}}; |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + t = Integer.parseInt(br.readLine()); |
| 17 | + for(int i=0; i<t; i++){ |
| 18 | + st = new StringTokenizer(br.readLine(), " "); |
| 19 | + m = Integer.parseInt(st.nextToken()); |
| 20 | + n = Integer.parseInt(st.nextToken()); |
| 21 | + k = Integer.parseInt(st.nextToken()); |
| 22 | + graph = new int[n][m]; |
| 23 | + for(int j=0; j<k; j++){ |
| 24 | + st = new StringTokenizer(br.readLine(), " "); |
| 25 | + a = Integer.parseInt(st.nextToken()); |
| 26 | + b = Integer.parseInt(st.nextToken()); |
| 27 | + graph[b][a] = 1; |
| 28 | + } |
| 29 | + int ans = 0; |
| 30 | + for(int x=0; x<graph.length; x++){ |
| 31 | + for(int y=0; y<graph[x].length; y++){ |
| 32 | + if(graph[x][y] == 1){ |
| 33 | + dfs(x, y); |
| 34 | + ans += 1; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + System.out.println(ans); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static void dfs(int x, int y){ |
| 43 | + graph[x][y] = 0; |
| 44 | + for(int i=0; i<4; i++){ |
| 45 | + int nx = x + dir[i][0]; |
| 46 | + int ny = y + dir[i][1]; |
| 47 | + if(nx < 0 || ny < 0 || nx >= n || ny >= m){ |
| 48 | + continue; |
| 49 | + } |
| 50 | + if(graph[nx][ny] == 1){ |
| 51 | + dfs(nx, ny); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
4 | 55 | }
|
0 commit comments