문제 링크 : https://www.acmicpc.net/problem/15683



문제 접근


1. 완전탐색으로 cctv의 방향이 바뀌는 모든 경우를 체크하여 사각지대가 가장 적은 경우의 사각지대 수를 출력하였다.


2. 솔직히 완전탐색은 풀이를 어케 써야될지 모르겠다. 그냥 가능한 경우를 모두 봐주면 된다.




소스 코드


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
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.util.ArrayList;
import java.util.Scanner;
 
public class Main {
    
    static int[][] map;
    static int n, m, mincnt;
    static ArrayList<Cctv> ar = new ArrayList<Cctv>();
    static int[] dy = { -1010 };
    static int[] dx = { 010-1 };
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        n = sc.nextInt();
        m = sc.nextInt();
        
        map = new int[n][m];
        
        mincnt = n*m;
        
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                int t = sc.nextInt();
                map[i][j] = t;
                if(t>=1 && t<=5) {
                    ar.add(new Cctv(i, j, t));
                }
            }
        }
        
        recur(0);
        
        System.out.println(mincnt);
    }
    
    static void recur(int index) {
        if(index==ar.size()) {
            int cnt=0;
            
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) {
                    if(map[i][j]==0) cnt++;
                }
            }
            
            if(mincnt > cnt)
                mincnt = cnt;
            
            return;
        }
        
        Cctv tmp = ar.get(index);
        
        switch (tmp.cctvType) {
        case 1 :
            
            for(int i=0; i<4; i++) {
                int[][] tmpmap = new int[n][m];
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        tmpmap[j][k] = map[j][k];
                    }
                }
                
                fillcctv(tmp.y, tmp.x, i);
                
                recur(index+1);
                
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        map[j][k] = tmpmap[j][k];
                    }
                }
            }
            
            break;
            
        case 2 :
            
            for(int i=0; i<2; i++) {
                int[][] tmpmap = new int[n][m];
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        tmpmap[j][k] = map[j][k];
                    }
                }
                
                fillcctv(tmp.y, tmp.x, i);
                fillcctv(tmp.y, tmp.x, i+2);
                
                recur(index+1);
                
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        map[j][k] = tmpmap[j][k];
                    }
                }
            }
            
            break;
            
        case 3 :
            
            for(int i=0; i<4; i++) {
                int[][] tmpmap = new int[n][m];
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        tmpmap[j][k] = map[j][k];
                    }
                }
                
                fillcctv(tmp.y, tmp.x, i);
                if(i==3)
                    fillcctv(tmp.y, tmp.x, 0);
                else
                    fillcctv(tmp.y, tmp.x, i+1);
                
                recur(index+1);
                
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        map[j][k] = tmpmap[j][k];
                    }
                }
            }
                
            break;
            
        case 4 : 
            
            for(int i=0; i<4; i++) {
                
                int[][] tmpmap = new int[n][m];
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        tmpmap[j][k] = map[j][k];
                    }
                }
                
                for(int j=0; j<4; j++) {
                    if(i==j) continue;
                    fillcctv(tmp.y, tmp.x, j);
                }
                
                recur(index+1);
                
                for(int j=0; j<n; j++) {
                    for(int k=0; k<m; k++) {
                        map[j][k] = tmpmap[j][k];
                    }
                }
            }
            
            break;
            
        case 5 : 
            
            for(int j=0; j<4; j++) {
                fillcctv(tmp.y, tmp.x, j);
            }
            
            recur(index+1);
            
        }
        
        
        
    }
    
    
    static void fillcctv(int y, int x, int d) {
        switch(d) {
        
        case 0 :
            for(int i = y-1; i>=0; i--) {
                if(map[i][x]==6break;
                if(map[i][x]>=1 && map[i][x]<=5continue;
                
                map[i][x] = 9;
            }
            
            break;
            
        case 1 :
            for(int i=x+1; i<m; i++) {
                if(map[y][i]==6break;
                if(map[y][i]>=1 && map[y][i]<=5continue;
                
                map[y][i] = 9;
            }
            break;
            
        case 2 :
            for(int i=y+1; i<n; i++) {
                if(map[i][x]==6break;
                if(map[i][x]>=1 && map[i][x]<=5continue;
                
                map[i][x] = 9;
            }
            break;
            
        case 3 :
            for(int i=x-1; i>=0; i--) {
                if(map[y][i]==6break;
                if(map[y][i]>=1 && map[y][i]<=5continue;
                
                map[y][i] = 9;
            }
            
            break;
        }
    }
    
}
 
 
class Cctv {
    int y;
    int x;
    int cctvType;
    
    Cctv(int y, int x, int cctvType) {
        this.y = y;
        this.x = x;
        this.cctvType = cctvType;
    }
}
cs



'Algorithm > 백준' 카테고리의 다른 글

백준 15685번 드래곤 커브  (0) 2019.05.02
백준 14891번 톱니바퀴  (0) 2019.04.30
백준 14889번 스타트와 링크  (0) 2019.04.28
백준 14890번 경사로  (0) 2019.04.28
백준 14503번 로봇 청소기  (0) 2019.04.27

+ Recent posts