티스토리 뷰

문제

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

입력

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

출력

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

 

문제 풀이

house_cnt 변수로 총 단지수를 카운팅함과 동시에 각 단지내 집에 house_cnt 값을 부여한다.

그러면 같은 단지에 있는 집들은 같은 값의 수를 갖게 된다.

1012번 유기농 배추와 유사한 풀이법을 갖는다.

 

각 단지내 집의 수를 오름차순으로 정렬하기 위해 bubble sort 방법을 사용했다.

 

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
#include<stdio.h>
 
#define SZ 26
 
int vectX[4= { 001-1 };
int vectY[4= { 1-100 };
 
int map[SZ][SZ];
int house_size;
int house_cnt = 0;
 
// 단지를 저장할 배열 = 나올 수 있는 단지의 최대 개수 = n * (n/2) + 1
int house[SZ*(SZ / 2+ 1];
 
void swap(int* arr, int left, int right) {
    int temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
}
 
void bubble_sort(int* arr) {
    for (int i = 0; i < house_cnt; i++) {
        for (int j = 0; j < house_cnt - 1; j++) {
            if (arr[j] > arr[j+1]) {
                swap(arr, j, j+1);
            }
        }
    }
}
 
void do_dfs(int x, int y, int key) {
    // key : 단지 개수를 의미.. 단지별 번호 이 값이 최종 단지의 개수가 된다.
    map[x][y] = key;
 
    int nextX; int nextY;
    for (int i = 0; i < 4; i++) {
        nextX = x + vectX[i];
        nextY = y + vectY[i];
 
        if (nextX >= 0 && nextY >= 0 && nextX < house_size && nextY < house_size) {
            //printf("map[%d][%d] = %d\n", nextX, nextY, map[nextX][nextY]);
            if (map[nextX][nextY] == 1) {
                // 좌표가 범위 안에 있으며 (x, y) 에서 (nextX, nextY)까지 가는 길이 있는 경우
                do_dfs(nextX, nextY, key);
            }
        }
    }
}
 
void solution() {
    for (int i = 0; i < house_size; i++) {
        for (int j = 0; j < house_size; j++) {
            if (map[i][j] == 1) {
                house_cnt++;
                do_dfs(i, j, house_cnt+1);
            }
        }
    }
 
    for (int i = 0; i < house_size; i++) {
        for (int j = 0; j < house_size; j++) {
            if (map[i][j] > 1) {
                house[map[i][j] - 2++;
            }
        }
    }
}
 
int main() {
    scanf("%d"&house_size);
    for (int i = 0; i < house_size; i++) {
        for (int j = 0; j < house_size; j++) {
            scanf("%1d"&map[i][j]);
        }
    }
 
    solution();
    printf("%d\n", house_cnt);
    bubble_sort(house);
    for (int i = 0; i < house_cnt; i++) {
        printf("%d\n", house[i]);
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
댓글