반응형

문제 출처 :


https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV18LoAqItcCFAZN



알고리즘 분석 :


문제 해결에 필요한 사항

1. 정렬

2. BFS


BFS를 통해 각 면적의 가로,세로를 구해주고 크기는 가로*세로로 구한다.


그리고 정렬에서 compare 함수를 이용하여 크기가 작은 순, 크기가 같다면 행이 작은 순으로 만든다.







소스 코드 : 


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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
using namespace std;
#define max(a,b)(a > b ? a : b)
 
 
int arr[102][102];
bool visit[102][102];
int dy[4= { -1,0,1,};
int dx[4= { 0,-1,0,};
 
struct INFO {
    int y, x, sz;
    int height, width;
};
struct TUPLE {
    int y, x, sz;
};
TUPLE ans[102];
TUPLE tmp[102];
int idx;
 
INFO q[1000002];
int first, last;
void init() {
    for (int i = 0; i < 102; i++)
        for (int j = 0; j < 102; j++)
            visit[i][j] = false;
    first = last = 0;
    idx = 0;
}
 
bool comp(int p, int q) {
    if (ans[p].sz == ans[q].sz)
        return ans[p].y < ans[q].y;
    return ans[p].sz < ans[q].sz;
}
void mergeSort(int s, int e) {
    if (s >= e)
        return;
 
    int m = (s + e) / 2;
 
    mergeSort(s, m);
    mergeSort(m + 1, e);
 
    int s1 = s, s2 = m + 1;
    int idx = s;
 
    while (s1 <= m || s2 <= e) {
        if (s2 > e || (s1 <= m && comp(s1, s2)))
            tmp[idx++= ans[s1++];
        else
            tmp[idx++= ans[s2++];
    }
    for (int i = s; i <= e; i++)
        ans[i] = tmp[i];    
}
int main() {
    freopen("input.txt""r", stdin);
    int tCase;
    scanf("%d"&tCase);
 
    for (int tc = 1; tc <= tCase; tc++) {
        init();
 
        int n;
        scanf("%d"&n);
 
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                scanf("%d"&arr[i][j]);
 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!arr[i][j] || visit[i][j])
                    continue;
 
                q[last++= { i,j,1,1,};
                int maxHeight = -1, maxWidth = -1;
                while (first < last) {
                    INFO here = q[first++];
                    if (visit[here.y][here.x])
                        continue;
 
                    visit[here.y][here.x] = true;
 
                    for (int i = 0; i < 4; i++) {
                        int ny = here.y + dy[i];
                        int nx = here.x + dx[i];
                        if (<= ny && ny < n && <= nx && nx < n && arr[ny][nx]) {
 
                            maxHeight = max(here.height + dy[i], maxHeight);
                            maxWidth = max(here.width + dx[i], maxWidth);
                            q[last++= { ny,nx,here.sz + 1, here.height + dy[i], here.width + dx[i] };
                        }
                    }
                }
 
                ans[idx++= { maxHeight, maxWidth, maxHeight * maxWidth };
            }
        }
 
 
        mergeSort(0, idx - 1);
 
        printf("#%d %d ", tc, idx);
        for (int i = 0; i < idx; i++)
            printf("%d %d ", ans[i].y, ans[i].x);
        printf("\n");
    }
 
 
    return 0;
}
 
cs

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[4803번] 트리  (4) 2019.08.03
[1245번] 균형점  (0) 2019.07.31
[1264번] 이미지 유사도 검사  (2) 2019.07.17
[1351번] 무한 수열  (0) 2019.07.14
[1808번] 지희의 고장난 계산기  (0) 2019.07.06