본문 바로가기
삼성SW아카데미/[SW Test 샘플문제]

1770. [SW Test 샘플문제] 블록 부품 맞추기

by 2744m 2019. 9. 26.
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com

링크 : 1770. [SW Test 샘플문제] 블록부품 맞추기

main을 보면 블록의 높이는 1~8인 것을 알 수 있다. int max_ =1+ (rand() % 6); module[c][y][x] = max_ + (rand() % 3);

블록 모양을 배열이 아닌 다른 방법으로 저장해서 빠르게 연산할 방법으로 16자리 10진수 각각의 자리에 높이를 저장하였다. (친구가 도움을 준 아이디어)

예를 들어 위 모양의 블록을 long long shape = 3222122313132231; 이런식으로 저장을 했다.

이런식으로 원래 모양을 저장하고 블록을 맞춰 끼우기 위해선 0, 90, 180, 270도 씩 돌려가면서 맞춰보면된다.

여기서 주의할 점은 맞춰 끼워보는 대상은 뒤집어서 끼워봐야 한다.

블록을 뒤집어서 4방향으로 돌려가면서 모양을 저장해서 shape를 기준으로 정렬을 한다.

현재 블록에서 최대로 합칠수 있는 블록 모양의 경우를 구해서 비교대상 리스트에서 바이너리 서치를 통해서 찾아보고,

리스트에 있으면 현재 블록과 찾은 블록을 합친 높이를 더해가고

리스트에 없으면 전체 높이를 1씩 줄여서 최대 높이가 3일 때 까지 찾아본다.

속도는 약1,600ms 정도 나왔는데 1,000ms 미만으로 푼 사람들이 많다..

더 공부해야겠다...

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#define MAX 30000
 
using namespace std;
 
extern int makeBlock(int module[][4][4]);
 
int main(void)
{
    static int module[MAX][4][4];
 
    srand(3); // 3 will be changed
 
    for (int tc = 1; tc <= 10; tc++)
    {
        for (int c = 0; c < MAX; c++)
        {
            int max_ = 1 + (rand() % 6);
            for (int y = 0; y < 4; y++)
            {
                for (int x = 0; x < 4; x++)
                {
                    module[c][y][x] = max_ + (rand() % 3);
                }
            }
        }
        cout << "#" << tc << " " << makeBlock(module) << endl;
    }
 
    return 0;
}
 
 
#define key 1111111111111111
typedef long long ll;
 
struct B {
    int num;
    ll shp;
    int max_;
    bool operator < (B ex) {
        if (ex.shp > shp) return true;
        else if (ex.shp == shp) return num < ex.num;
        else return false;
    }
    bool operator == (B ex) {
        if (ex.shp == shp) return true;
        else return false;
    }
    B &operator = (B ex) {
        num = ex.num;
        shp = ex.shp;
        max_ = ex.max_;
        return *this;
    }
};
 
B origin[30000 + 10];
B change_shp[30000 * 4 + 10];
int used_chk[30000 + 10];
B reset;
 
int BS(ll target) {
    int f = 0, r = 30000 * 4 - 1;
    int mid;
    while (f <= r) {
        mid = (f + r) / 2;
        if (change_shp[mid].shp == target)
            return change_shp[mid].num;//맞는 블록 고유 넘버 반환
        if (change_shp[mid].shp < target)
            f = mid + 1;
        else
            r = mid - 1;
    }
    return -1;//찾지 못했다.
}
 
 
B tmp_arr[30000 * 4];
 
void merge(int left, int mid, int right) {
    int i, j, k;
    i = left;
    j = mid + 1;
    k = left;
    while (i <= mid && j <= right) {
        if ((change_shp[i] < change_shp[j]) || (change_shp[i] == change_shp[j])) {
            tmp_arr[k] = change_shp[i];
            i++;
        }
        else {
            tmp_arr[k] = change_shp[j];
            j++;
        }
        k++;
    }
    if (i > mid) {
        for (int m = j; m <= right; m++) {
            tmp_arr[k] = change_shp[m];
            k++;
        }
    }
    else {
        for (int m = i; m <= mid; m++) {
            tmp_arr[k] = change_shp[m];
            k++;
        }
    }
    for (int m = left; m <= right; m++) {
        change_shp[m] = tmp_arr[m];
    }
}
void merge_sort(int left, int right) {
    int mid;
    if (left < right) {
        mid = (left + right) / 2;
        merge_sort(left, mid);
        merge_sort(mid + 1, right);
        merge(left, mid, right);
    }
}
 
 
void init() {
    for (int i = 0; i < 30000*4; i++) {
        if (i < 30000) {
            used_chk[i] = 0;
            origin[i] = reset;
        }
        change_shp[i] = reset;
        tmp_arr[i] = reset;
    }
}
int makeBlock(int module[][4][4]) {
    int ans = 0;
    int block_cnt = 0;
 
    init();
 
    for (int i = 0; i < 30000; i++) {
        ll make_origin_shp = 0;
        ll make_reverse_shp = 0;
        ll mul = 1;
        int now_shp_tmp[4][4];
        int rotate_arr_tmp[4][4];
        int max_h = 0;
        for (int r = 0; r < 4; r++) {
            for (int c = 0; c < 4; c++) {
                make_origin_shp += module[i][3 - r][3 - c] * mul;
                mul *= 10;
                now_shp_tmp[r][3 - c] = module[i][r][c];
                max_h = max_h < module[i][r][c] ? module[i][r][c] : max_h;
            }
        }
 
        origin[i].num = i;
        origin[i].shp = make_origin_shp;
        origin[i].max_ = max_h;
 
        for (int rotate = 0; rotate < 4; rotate++) {
            for (int r = 0; r < 4; r++) {
                for (int c = 0; c < 4; c++) {
                    rotate_arr_tmp[3 - c][r] = now_shp_tmp[r][c];
                }
            }
            mul = 1;
            make_reverse_shp = 0;
            for (int r = 0; r < 4; r++) {
                for (int c = 0; c < 4; c++) {
                    make_reverse_shp += rotate_arr_tmp[r][c] * mul;
                    mul *= 10;
                    now_shp_tmp[r][c] = rotate_arr_tmp[r][c];
                }
            }
 
            change_shp[block_cnt].num = i;
            change_shp[block_cnt].shp = make_reverse_shp;
            change_shp[block_cnt].max_ = max_h;
            block_cnt++;
        }
    }
    //원래 블록들 모양 저장 and 회전시킨 비교군(뒤집은거) 저장완료
 
    //sort(change_shp, change_shp + 30000 * 4);
    
    merge_sort(030000 * 4 - 1);
 
    for (int i = 0; i < 30000; i++) {
        ll match_block = (origin[i].max_ + 6)*key - origin[i].shp;//육면체를 만들수 있는 최대 높이 블록
        int match_block_max_h = 8;
        while (1) {
            int BS_result = BS(match_block);
            if (BS_result > -1) {//찾았다
                if (used_chk[BS_result] == 0 && BS_result != i) {//이 블록이 사용을 하지 않은 블록이면
                    used_chk[BS_result] = -1;//블록 사용 체크
                    used_chk[i] = -1;
                    ans += match_block % 10 + origin[i].shp % 10;//만든 블록 높이
                    break;
                }
                else {//이미 사용된 블록
                    match_block -= key;
                    match_block_max_h--;
                }
            }
            else {//못찾음
                match_block -= key;
                match_block_max_h--;
            }
            if (match_block_max_h == 2break;//최대 높이가 2이면 종료
        }
    }
    return ans;
}
 
cs

댓글