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
230
231
232
233
234
235
236
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution {
 
    private static int tc;
    private static int bomb;
    private static int width, height;
    private static int[][] map;
    private static int[][] copymap;
    private static int[][] visit; 
    private static int ans = Integer.MAX_VALUE;
    private static boolean range(int x, int y) {
        if (x >= height || x < 0 || y >= width || y < 0)
            return false;
        else
            return true;
    }
 
    private static void DFSbomb(int x, int y, int range) {
 
        visit[x][y] = 1;
        if (map[x][y] == 1) {
            map[x][y] = 0;
        } else if (map[x][y] == 0) {
            ;
        } else if (map[x][y] >= 1) {
            // 상
            int up = 1;
            int upx = x;
            int tempx = upx;
            while (up < map[tempx][y] && range(upx, y) ) {
                upx -= 1;
                if (range(upx, y) && visit[upx][y] == 0) {
                    DFSbomb(upx, y, map[upx][y]);
                    up += 1;
                }else
                    up+=1;
            } // expect : 위로 올라가면서 1보다 값이 크면 상을 또 볼것이다.
 
            // 하
            int down = 1;
            int downx = x;
            tempx = downx;
            while (down < map[tempx][y] && range(downx, y) ) {
                downx += 1;
                if (range(downx, y) && visit[downx][y]==0) {
                    DFSbomb(downx, y, map[downx][y]);
                    down += 1;
                }else
                    down+=1;
            }
 
            // 좌
            int left = 1;
            int lefty = y;
            int tempy = lefty;
            while (left < map[x][tempy] && range(x, lefty)) {
                lefty -= 1;
                if (range(x, lefty) && visit[x][lefty]==0) {
                    DFSbomb(x, lefty, map[x][lefty]);
                    left += 1;
                }else
                    left+=1;
            }
 
            // 우
            int right = 1;
            int righty = y;
            tempy = righty;
            while (right < map[x][tempy] && range(x, righty)) {
                righty += 1;
                if (range(x, righty) && visit[x][righty]==0) {
                    DFSbomb(x, righty, map[x][righty]);
                    right += 1;
                }else
                    right+=1;
            }
             
            map[x][y] = 0;
        }
    }
 
    private static void DFS(int[] bombpos, int n) {
        if (n == bomb) {
            // bomb개수 만큼 터트리자.
            for (int i = 0; i < bombpos.length; i++) {
                //if(bombpos[0] == 2 && bombpos[1] == 2 && bombpos[2] ==7  )
                //  System.out.println("debug--");
                //System.out.print(bombpos[i]+" ");
                //System.out.println();
                int x = 0;
                int y = bombpos[i];
                // 폭탄을 놓을 y값 선정
 
                k:while (true) {
                    if(map[x][y]>=1)
                        ;
                    else
                        x+=1;
                    if (range(x, y)) {
                        // 만약에 범위 안이라면
                        if (map[x][y] >= 1) {
                            // 1보다 값이 크다면
                            visit[x][y]=1;
                            // 상,하,좌,우 다 확인한다.
                            // 상
                            int up = 1;
                            int upx = x;
                            while (up < map[x][y] && range(upx, y)) {
                                upx -= 1;
                                if (range(upx, y)) {
                                    DFSbomb(upx, y, map[upx][y]);
                                    up += 1;
                                }
                            } // expect : 위로 올라가면서 1보다 값이 크면 상을 또 볼것이다.
 
                            // 하
                            int down = 1;
                            int downx = x;
                            while (down < map[x][y] && range(downx, y)) {
                                downx += 1;
                                if (range(downx, y)) {
                                    DFSbomb(downx, y, map[downx][y]);
                                    down += 1;
                                }
                            }
 
                            // 좌
                            int left = 1;
                            int lefty = y;
                            while (left < map[x][y] && range(x, lefty)) {
                                lefty -= 1;
                                if (range(x, lefty)) {
                                    DFSbomb(x, lefty, map[x][lefty]);
                                    left += 1;
                                }
                            }
 
                            // 우
                            int right = 1;
                            int righty = y;
                            while (right < map[x][y] && range(x, righty)) {
                                righty += 1;
                                if (range(x, righty)) {
                                    DFSbomb(x, righty, map[x][righty]);
                                    right += 1;
                                }
                            }
                            visit = new int[height][width];
                            map[x][y] = 0;
                            break k;
                        }
                    } else
                        break k; // 범위 벗어났다.
                     
                }
                 
                //수직 낙하!! 
                 
                 
                for (int b = height-1; b >=0; b--) {
                    for (int bb = width-1; bb >=0; bb--) {
                        if(map[b][bb]!=0) {
                            int temp = map[b][bb];
                            int cc = b;
                            cc+=1;
                            while(range(cc,bb) && map[cc][bb]==0) {
                                 
                                map[cc][bb] = temp;
                                map[cc-1][bb] = 0;
                                temp = map[cc][bb];
                                cc+=1;
                            }
                        }
                    }
                } // end of for loop
                int c = 0;
                for (int si = 0; si < height; si++) {
                    for (int j = 0; j < width; j++) {
                        if(map[si][j]>=1)
                            c+=1;
                    }
                     
                } // end of for loop
 
                if(ans>c)
                    ans = c;
                 
            }
 
             
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    map[i][j] = copymap[i][j];
                }
            } // end of for loop
        } else {
            for (int i = 0; i < width; i++) {
                bombpos[n] = i;
                DFS(bombpos, n + 1);
            }
        }
    }
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        tc = Integer.parseInt(br.readLine());
        for(int sst=1; sst<=tc; sst++) {
        StringTokenizer st = new StringTokenizer(br.readLine());
 
        bomb = Integer.parseInt(st.nextToken());
        width = Integer.parseInt(st.nextToken());
        height = Integer.parseInt(st.nextToken());
        visit = new int[height][width];
         
        map = new int[height][width];
        copymap = new int[height][width];
        for (int i = 0; i < height; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < width; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                copymap[i][j] = map[i][j];
            }
        } // end of for loop
 
        int[] bombpos = new int[bomb];
        DFS(bombpos, 0);
        System.out.println("#"+sst+" "+ans);
        ans = Integer.MAX_VALUE;
        }
    }
 
}
cs

 

1. width 에서 좌표 3개를 선택

2. 위에서 부터 전수 조사해서  map에 저장된 값이 1 이상이라면 (여기서 방문체크 => 없다면 스택오버 플로우)

3. 상, 하, 좌, 우 로 범위를 체크 

4. 한번 폭탄을 터트리고 아래서부터 확인해서 height 범위에서 내려갈 수 있고 0이라면 아래로 밀기

 

완전탐색 + 시뮬레이션 문제