공부공간

BOJ - 17136 ) 색종이 붙이기 본문

알고리즘/완전탐색(BFS,DFS)

BOJ - 17136 ) 색종이 붙이기

개발자가될수있을까? 2020. 2. 4. 08:39


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

 

17136번: 색종이 붙이기

<그림 1>과 같이 정사각형 모양을 한 다섯 종류의 색종이가 있다. 색종이의 크기는 1×1, 2×2, 3×3, 4×4, 5×5로 총 다섯 종류가 있으며, 각 종류의 색종이는 5개씩 가지고 있다. <그림 1> 색종이를 크기가 10×10인 종이 위에 붙이려고 한다. 종이는 1×1 크기의 칸으로 나누어져 있으며, 각각의 칸에는 0 또는 1이 적혀 있다. 1이 적힌 칸은 모두 색종이로 덮여져야 한다. 색종이를 붙일 때는 종이의 경계 밖으로 나가서는 안되고, 겹쳐

www.acmicpc.net

 

 


한참 시간초과가 난문제..

 

색종이를 붙일수있으면 ( 5x5,4x4...) 붙이고 최소로 덮을 수 있는 개수를 출력하는 

 

문제이다. 

 

각 색종이는 5장씩있어서 5장이 넘어가서 덮는 경우에는 -1을 출력한다.

 

처음에 완전탐색으로 모든경우( 최적해를 구하기위해 ) 를탐색했는데 자꾸 시간초과가 났다.

 

가장 큰 것부터 붙이기 때문에 그리디한 해가 가장 먼저 나오게된다.

 

탐색을 돌다가 그 해가 넘어가는 경우는 함수를 바로 끝내주어야한다 ( 백트래킹 )

 

이 수를 생각못해서 3시간넘게 고민했던거같다 ..ㅠ

 

 


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
 
public class Main {
    static int map[][] = new int[11][11];
    static int color_square[] = {555555};
    static int ans = Integer.MAX_VALUE;
    static boolean visit[][] = new boolean[11][11];
    
    public static void input()throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for(int y = 0;y < 10 ;y ++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for(int x = 0; x <10 ; x++) {
                map[y][x] = Integer.parseInt(st.nextToken());
            }
        }
    }
    
    public static void main(String[] args) throws Exception{
        input();
        square(0,0);
        if(ans != Integer.MAX_VALUE)System.out.println(ans);
        else System.out.println(-1);
    }
 
    public static void square(int pos, int cnt) {
        int y = pos/10;
        int x = pos%10;
        if(ans <= cnt) return;
        if(pos ==100) {
            if(check()){
                int res = 0;
                for (int i : color_square)res += (5-i);
                ans = Math.min(ans, res);
                return;
            }
        }
        
        if(map[y][x] == 1) {
            for(int color = 5; color > 0 ; color-=1 ) {
                if(color_simul(y,x,color) && color_square[color] > 0) { // 채울수 있으면?
                    color_fill(y,x,color,0); // 1로 채우고
                    color_square[color]--;   // color번째 요소를 감소 시킨다.
                    square(pos+1,cnt+1);
                    color_fill(y,x,color,1); // 갔다왔으니 0으로 채우고 
                    color_square[color]++;   // 다시 증가시킨다.
                }
            }    
        }
        
        else square(pos+1,cnt);
        
    }
    public static boolean check() {
        for(int y = 0; y <10  ; y++) {
            for(int x = 0; x <10; x++) {
                if(map[y][x] == 1return false;
            }
        }
        return true;
    }
 
    public static void color_fill(int y, int x, int color, int i) {
        for(int y_ = y; y_ < y+color ; y_++) {
            for(int x_ = x; x_ <x+color; x_++) {
                if(y_ < 10 && x_<10) map[y_][x_] = i;
            }
        }
    }
 
    public static boolean color_simul(int y, int x, int color) {
        if(y + color > 10 || x + color > 10return false;
        for(int y_ =y ; y_ < y+color ; y_++) {
            for(int x_=x ; x_ < x+color ; x_++) {
                if(y_ < 10 && x_< 10 && map[y_][x_] ==0return false;
            }
        }
        return true;
    }
 
}
 
 

'알고리즘 > 완전탐색(BFS,DFS)' 카테고리의 다른 글

SWEA ) 벽돌깨기  (0) 2020.02.09
SWEA ) 등산로 조성  (0) 2020.02.05
BOJ - 17135 ) 캐슬디펜스  (2) 2020.02.02
BOJ - 16637 ) 괄호추가하기  (0) 2020.02.02
BOJ - 2644 ) 촌수계산  (0) 2020.01.28
Comments