알고리즘/백준

백준 15683 - 감시 (JAVA)

체제 2022. 9. 5. 23:07

출처 - 백준프로그램


[ 알고리즘 ] 

1번  : 상 / 하 / 좌 / 우  중 한방향으로 쭉 ( 벽을 만나거나 경계만날때까지 ) 

2번 : (상, 하) / (좌, 우 ) 

3번 : (상,우) (우,하) (하,좌), (좌,상)

4번 : (좌,상,우) (상,우,하), (우,하,좌), (하,좌,상)

5번 : (상,하,좌,우)

 

CCTV 의 위치를 모두 저장해두고, 가능한 모든 경우의 수를 완전탐색한다. 

dfs 를 통해 재귀 호출하며 탐색하고, 종료시 0의 갯수를 세서 매번 최소 사각지대 갯수를 갱신한다. 

 

+ 간단해보이지만, 구현이 은근히 어려웠다. 

한쪽으로 탐색하며 dfs 호출하는게 생각보다 쉽지 않았기 때문에,

상 / 하 / 좌 / 우 한방향을 경계만날때까지 탐색하는 함수를 따로 구현하였다. (goRight ~ goUp) 

→ 다른 구현 문제에서도 유익하게 사용 될 것 같다 .

CCTV 갯수가 여러개 이기때문에 , 각 CCTV 에 대한 탐색을 마칠때마다, 변경된 map 을 가지고 다녀야한다.

다음 탐색을 위해 copyMap 으로 매번 map 을 탐색할 때 마다 변경해주었다. 

 

 

[ 코드 ]

 

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
import java.io.*;
import java.util.*;
 
public class Solution_BaekJoon_15683_골드4_감시 {
    static int min = Integer.MAX_VALUE;
    static List<camera> list = new LinkedList<>();
    private static int[][] map;
    private static int r;
    private static int c; 
    static class camera{
        int type; //1~5 
        int x; int y;
 
 
        public camera(int type, int x, int y) {
            this.type = type;
            this.x = x;
            this.y = y;
        } 
    }
    public static void main(String[] args) throws Exception{
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st ;
        
        String s= br.readLine();
        st= new StringTokenizer(s, " ");
        
        r = Integer.parseInt(st.nextToken());
        c = Integer.parseInt(st.nextToken());
        
        map = new int[r][c];
        
        for(int i=0; i<r; i++) {
            s= br.readLine();
            st= new StringTokenizer(s, " ");
            for(int j=0; j<c; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                // cctv 인 부분은, 어떤 카메라인지와 좌표 저장 
                if(map[i][j]>=1 && map[i][j]<=5) {
                    list.add(new camera(map[i][j], i, j)); 
                }
            }
        }
        /*-----------------end of input---------------------*/
 
        dfs(0, map);
        
        System.out.println(min);
        
        
    }// end of main 
    private static void dfs(int index, int[][] map) {
        if(index == list.size()) {
            int count = 0
            for(int i=0; i<r; i++) {
                for(int j=0; j<c; j++) {
                    if(map[i][j] ==0) count++;  
                }
            }
            min= Math.min(count, min);
            return ; 
        }
        
        camera ca= list.get(index);
        int x= ca.x;
        int y= ca.y;
        int type= ca.type;
        int[][] maps;
        switch(type) {
        // 한방향으로 가기 
        case 1:
            maps= copyMap(map);
            goLeft(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goRight(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goUp(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goDown(maps,x,y);
            dfs(index+1,maps);
            
            break;
            
        // 상 하 또는 좌 우 
        case 2:
            maps= copyMap(map);
            goLeft(maps,x,y);
            goRight(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goUp(maps,x,y);
            goDown(maps,x,y);
            dfs(index+1,maps);
            
            break;
        // (상,우) (우,하) (하,좌), (좌,상)    
        case 3:
            maps= copyMap(map);
            goUp(maps,x,y);
            goRight(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goRight(maps,x,y);
            goDown(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goDown(maps,x,y);
            goLeft(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goLeft(maps,x,y);
            goUp(maps,x,y);
            dfs(index+1,maps);
            
            break;
            
        //(좌,상,우) (상,우,하), (우,하,좌), (하,좌,상)    
        case 4:
            maps= copyMap(map);
            goLeft(maps,x,y);
            goUp(maps,x,y);
            goRight(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goUp(maps,x,y);
            goRight(maps,x,y);
            goDown(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goRight(maps,x,y);
            goDown(maps,x,y);
            goLeft(maps,x,y);
            dfs(index+1,maps);
            
            maps= copyMap(map);
            goDown(maps,x,y);
            goLeft(maps,x,y);
            goUp(maps,x,y);
            dfs(index+1,maps);
            
            break;
        
        //상 하 좌 우 
        case 5:
            maps= copyMap(map);
            goDown(maps,x,y);
            goLeft(maps,x,y);
            goUp(maps,x,y);
            goRight(maps,x,y);
            dfs(index+1,maps);
            
            break;
            
            
        }
        
    }
    private static void goLeft(int[][]maps, int x, int y) {
        for(int i=y-1; i>=0; i--) {
            if(maps[x][i] == 6break//벽 만났음 
            if(maps[x][i] !=0continue// 다른 cctv 
            maps[x][i]=7//cctv 설치 
        }
    }
    private static void goRight(int[][]maps, int x, int y) {
        for(int i=y+1; i<c ; i++) {
            if(maps[x][i] == 6break//벽 만났음 
            if(maps[x][i] !=0continue// 다른 cctv 
            maps[x][i]=7//cctv 설치 
        }
    }
    private static void goUp(int[][]maps, int x, int y) {
        for(int i=x-1; i>=0 ; i--) {
            if(maps[i][y] == 6break//벽 만났음 
            if(maps[i][y] !=0continue// 다른 cctv 
            maps[i][y]=7//cctv 설치 
        }
    }
    private static void goDown(int[][]maps, int x, int y) {
        for(int i=x+1; i<r ; i++) {
            if(maps[i][y] == 6break//벽 만났음 
            if(maps[i][y] !=0continue// 다른 cctv 
            maps[i][y]=7//cctv 설치 
        }
    }
    
    private static int[][] copyMap(int[][] map) {
        int map1[][]= new int[r][c];
        for(int i=0; i<r; i++) {
            for(int j=0; j<c; j++) {
                map1[i][j] = map[i][j];
            }
        }
        return map1;
    }
}// end of class 
 
cs