-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
141 lines (128 loc) · 4.69 KB
/
Main.java
File metadata and controls
141 lines (128 loc) · 4.69 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int x,y;
static int[][] map;
static int[] dx={0,1,0,-1}; // 좌하우상
static int[] dy={-1,0,1,0};
static List<CCTV> cctvs;
static int allCount = 0;
static int monitorCount = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
y = Integer.parseInt(st.nextToken());
x = Integer.parseInt(st.nextToken());
map = new int[y][x];
cctvs = new ArrayList<CCTV>();
for(int i=0; i < y; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j < x; j++) {
int area = Integer.parseInt(st.nextToken());
if(area == 0) {
allCount++;
} else if(area != 6) {
cctvs.add(new CCTV(j,i,area));
}
map[i][j] = area;
}
}
backTracking(0, new CCTV[cctvs.size()]);
System.out.println(monitorCount);
br.close();
}
public static void backTracking(int idx, CCTV[] selectedCctvs) {
if(idx == cctvs.size()) {
countUnreachedArea(selectedCctvs, new boolean[y][x]);
} else {
CCTV curCctv = cctvs.get(idx);
for(int i=0; i<4; i++) {
CCTV c = new CCTV(curCctv.x, curCctv.y, curCctv.type);
switch(curCctv.type) {
case 1:
c.addDirection(i);
selectedCctvs[idx] = c;
backTracking(idx+1, selectedCctvs);
break;
case 2:
if(i>=2) { // 2가지 경우의 수 밖에 없음(상하 / 좌우)
return;
}
c.addDirection(i);
c.addDirection(i+2);
selectedCctvs[idx] = c;
backTracking(idx+1, selectedCctvs);
break;
case 3:
c.addDirection(i);
c.addDirection((i+1) % 4);
selectedCctvs[idx] = c;
backTracking(idx+1, selectedCctvs);
break;
case 4:
c.addDirection(i);
c.addDirection((i+1) % 4);
c.addDirection((i+2) % 4);
selectedCctvs[idx] = c;
backTracking(idx+1, selectedCctvs);
break;
case 5:
if(i>=1) { // 1가지 경우의 수 밖에 없음
return;
}
c.addDirection((i));
c.addDirection((i+1) % 4);
c.addDirection((i+2) % 4);
c.addDirection((i+3) % 4);
selectedCctvs[idx] = c;
backTracking(idx+1, selectedCctvs);
break;
}
}
}
}
// 사각지대 개수 검사
public static void countUnreachedArea(CCTV[] selectedCctvs, boolean[][] visited) {
int count = 0;
for(int i=0; i<selectedCctvs.length; i++) {
CCTV c = selectedCctvs[i];
for(int d=0; d<c.DirectionSize(); d++) {
int dir = c.directions.get(d);
int nx = c.x + dx[dir];
int ny = c.y + dy[dir];
while((nx >= 0 && nx < x) && (ny >= 0 && ny < y)) {
if(map[ny][nx] == 0) {
if(!visited[ny][nx]) {
visited[ny][nx] = true;
count++;
}
} else if(map[ny][nx] == 6) {
break;
}
nx += dx[dir];
ny += dy[dir];
}
}
}
monitorCount = Math.min(monitorCount, allCount - count);
}
}
class CCTV {
int x, y, type;
List<Integer> directions = new ArrayList<Integer>();
public CCTV(int x, int y, int type) {
this.x = x;
this.y = y;
this.type = type;
}
public void addDirection(int dir) {
directions.add(dir);
}
public int DirectionSize() {
return directions.size();
}
}