Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

혼종 꼬지마루

[백준] 17136 - 색종이 붙이기 본문

Algorithms/Brute Force

[백준] 17136 - 색종이 붙이기

꼬지마루 2019. 4. 7. 17:24

전형적인 Brute Force 문제이다.


색종이를 큰사이즈부터 가능한 위치에 붙여넣고, 모두 붙였다면 최소값을 찾는 문제


원래 나는 stl을 안쓰고 문제를 푸는것을 선호하지만, 이 문제는 안쓰고는 시간초과가 날 것 같아서 vector를 사용했다


1. map을 순회하며 색종이를 붙일 수 있는 위치를 찾는다.


2. 큰사이즈(5x5) 부터 작은 사이즈(1x1)를 차례대로 붙이면서 다음으로 넘어간다.


3. 만약 더이상 붙일 자리가 없다면 최소값을 갱신


4. 붙이는 갯수가 답보다 커진다면 return


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
#include <iostream>
#include <vector>
#define INF 987654321
using namespace std;
 
int checkcnt[6];
int dab = INF;
 
void dfs(int d, int x_, int
 y_, int sizevector< vector<int> > map) {
    bool dabcheck = true;
    if (d > dab) return;
    for (int i = 0; i < 10; i++){
        for (int j = 0; j < 10; j++){
            if (map[i][j] == 1) { dabcheck = falsebreak;}
        }
        if(!dabcheck) break;
    }
    if (dabcheck) {
        dab = dab > d - 1 ? d - 1 : dab;
        if (dab == -1) dab = 0;
        return;
    }
    for (int i = y_; i < y_ + size; i++) {
        for (int j = x_; j < x_ + size; j++) {
            if (i >= 10 || j >= 10 || map[i][j] == 0return;
            map[i][j] = 0;
        }
    }
    int x, y;
    bool flag = false;
    for (x = 0; x < 10; x++) {
        for (y = 0; y < 10; y++) {
            if (map[y][x] == 1) {
                flag = true;
                break;
            }
        }
        if (flag) break;
    }
    
    for (int i = 5; i >= 1; i--) {
        if (checkcnt[i] == 5continue;
        checkcnt[i]++;
        dfs(d + 1, x, y, i, map);
        checkcnt[i]--;
    }
}
 
int main(void) {
    std::vector< std::vector<int> > map(10vector<int>(100));
    for (int i = 0; i < 10; i++)
        for (int j = 0; j < 10; j++)
            cin >> map[i][j];
    dfs(0000, map);
    if (dab == INF) cout << -1 << endl;
    else cout << dab << endl;
    return 0;
}
 
cs


'Algorithms > Brute Force' 카테고리의 다른 글

[백준] 18192 - 보고 정렬  (0) 2019.12.15
[백준] 3980 - 선발 명단  (0) 2018.09.14
[백준] 1421 - 나무꾼 이다솜  (0) 2018.08.27