본문 바로가기
Algorithm/Baekjoon Online Judge

[2667] 단지번호붙이기

by All Here 2018. 11. 30.
반응형

https://www.acmicpc.net/problem/2667

 

2667번: 단지번호붙이기

<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여

www.acmicpc.net

문제

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

입력

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

출력

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

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Main {
    static int N;
    static int[][] MAP;
    static boolean[][] visited;
    static int[] dx= {-1,1,0,0};
    static int[] dy= {0,0,-1,1};
    static int count=0;
    static ArrayList<Integer> al = new ArrayList<Integer>();
    public static void main(String[] args) {
        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 str=sc.next();
            for(int j=0; j<N; j++) {
                MAP[i][j] = str.charAt(j)-48;
            }
        }
        
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(MAP[i][j]==1 && !visited[i][j]) {
                    count++;
                    bfs(i,j);
                }
            }
        }
        
        
        al.sort(null);
        
        System.out.println(al.size());
        
        
        for(int i=0; i<al.size(); i++) {
            System.out.println(al.get(i));
        }
        
    }
 
    private static void bfs(int x, int y) {
        Queue<HERE> q = new LinkedList<HERE>();
        int cnt=0;
        visited[x][y]=true;
        
        q.offer(new HERE(x,y));
        
        while(!q.isEmpty()) {
            cnt++;
            
            HERE h = q.poll();
            for(int i=0; i<4; i++) {
                int nextX = h.x + dx[i];
                int nextY = h.y + dy[i];
                
                if(nextX<0 || nextY<0 || nextX>=|| nextY>=N) continue;
                
                if(MAP[nextX][nextY]==1 && !visited[nextX][nextY]) {
                    visited[nextX][nextY]=true;
                    q.add(new HERE(nextX,nextY));
                }
            }
        }
        
        al.add(cnt);
    }
}
 
class HERE{
    int x;
    int y;
    
    HERE(int x, int y){
        this.x = x;
        this.y = y;
    }
}
 
cs

 

반응형

'Algorithm > Baekjoon Online Judge' 카테고리의 다른 글

[11559] Puyo Puyo  (0) 2018.12.06
[1260] DFS와 BFS  (0) 2018.12.06
[1012] 유기농 배추  (0) 2018.11.30
[10026] 적록색약  (0) 2018.11.30
[2178] 미로탐색  (0) 2018.11.30