본문 바로가기

Algorithm/백준

[백준 - 2667] 단지 번호 붙이기 - java

[백준 - 2667] 단지 번호 붙이기


문제 설명

<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다.
철수는 이 지도를 가지고 연결된 집들의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다.
여기서 연결되었다는 것은 어떤 집이 좌우, 혹은 아래위로 다른 집이 있는 경우를 말한다.
대각선상에 집이 있는 경우는 연결된 것이 아니다.
<그림 2>는 <그림 1>을 단지별로 번호를 붙인 것이다.
지도를 입력하여 단지수를 출력하고, 각 단지에 속하는 집의 수를 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오.

image

입력

첫 번째 줄에는 지도의 크기 N(정사각형이므로 가로와 세로의 크기는 같으며 5≤N≤25)이 입력되고, 그 다음 N줄에는 각각 N개의 자료(0혹은 1)가 입력된다.

출력

첫 번째 줄에는 총 단지수를 출력하시오. 그리고 각 단지내 집의 수를 오름차순으로 정렬하여 한 줄에 하나씩 출력하시오.

입력 예제

7
0110100
0110101
1110101
0000111
0100000
0111110
0111000

출력 예제

3
7
8
9

코드

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package baekJoon.BFS;
 
import java.util.*;
 
// 단지번호붙이기
public class B_2667 {
    
    private static ArrayList<Integer> list = new ArrayList<Integer>();
    private static int[][] map;
    private static boolean[][] visited;
    static int []dy = {01-10}; // 하상우좌
    static int []dx = {100-1};
    static int n;
 
    public static void solution () {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        map = new int[n][n];
        visited = new boolean[n][n];
 
        for(int i=0; i<n; i++) {
            String input = sc.next();
            for(int j=0; j<n; j++) {
                map[i][j] = input.charAt(j) - '0';
            }
        }
 
        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                if(map[i][j] == 1 && !visited[i][j]) {
                    bfs(i,j);
                }
            }
        }
 
        System.out.println(list.size());
        Collections.sort(list);
        for(int result : list) {
            System.out.println(result);
        }
 
    }
 
    private static void bfs (int y, int x) {
        Queue<int[]> queue = new LinkedList<int[]>();
        int count = 1;
        queue.offer(new int[] {y, x});
        visited[y][x] = true;
 
        while(!queue.isEmpty()) {
            int [] data = queue.poll();
            int curY = data[0];
            int curX = data[1];
 
            for(int i=0; i<4; i++) {
                int newY = curY + dy[i];
                int newX = curX + dx[i];
 
                if (newX >= 0 && newY >= 0 && newX < n && newY < n ) { // map 범위
                    if(map[newY][newX] != 0 && !visited[newY][newX]) {
                        visited[newY][newX] = true;
                        queue.offer(new int[]{newY, newX});
                        count++;
                    }
                }
            }
        }
        list.add(count);
    }
 
    public static void main(String[] args) {
        solution();
    }
}
 
cs

'Algorithm > 백준' 카테고리의 다른 글

[백준 - 1697] 숨바꼭질 - java  (0) 2020.05.25
[백준 - 7576] 토마토 - java  (0) 2020.05.24
[백준 - 1260] DFS와 BFS - java  (0) 2020.05.22
[백준 - 11051] 이항계수 2 - java  (0) 2020.05.19
[백준 - 1966] 프린터 큐 - java  (0) 2020.05.19