Search

'자바'에 해당되는 글 7건

  1. 2019.12.17 백준 1012 - 유기농 배추

백준 1012 - 유기농 배추

it/알고리즘 2019. 12. 17. 22:45 Posted by 하얀나다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;
public class Main{
    static int test;
    static int N,M;
    static int[][] map;
    static int cnt;
    static int[] dx = {-1100};
    static int[] dy = {00 , -1,1};
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        test = sc.nextInt();
        for(int tc = 0 ; tc < test; tc++){
            M = sc.nextInt();
            N = sc.nextInt();
            
            map = new int[N][M];
            Queue<Integer> que = new LinkedList<Integer>();
            cnt = sc.nextInt();
            
            for(int i = 0 ; i<cnt; i++){
                int y = sc.nextInt();
                int x = sc.nextInt();
                map[x][y] = 1;
            }
            
            for(int i = 0; i< map.length; i++){
                for(int j = 0 ; j < map[i].length; j++){
                    if(map[i][j] > 0){
                        DFS(i,j);
                        if(cnt>0){
                            que.add(cnt);
                            cnt = 0;
                        }
                    }
                }
            }
            System.out.println(que.size());
        }
    }
    
    private static void DFS(int x, int y){
        if(map[x][y] != 1){
            return;
        }
        
        cnt+=1;
        map[x][y] = 2;
        for(int i = 0 ; i< 4; i++){
            int xx = x+dx[i];
            int yy = y + dy[i];
            if(xx>=0 && yy>=0 && xx<map.length && yy<map[0].length){
                DFS(xx,yy);
            }
        }
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

'it > 알고리즘' 카테고리의 다른 글

백준 1110 - 더하기 사이클  (0) 2019.12.17
백준 1058 - 친구  (0) 2019.12.17
백준 10026 - 적록색약  (0) 2019.09.01
백준 11057 - 오르막 수  (0) 2019.09.01
백준 1309 - 동물원  (0) 2019.09.01