Skip to content

Commit a736b84

Browse files
committed
[20251018] BOJ / G3 / 감시 / 김민진
1 parent 4f3e1af commit a736b84

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
```java
2+
import java.io.*;
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_15683_감시 {
9+
10+
private static final int[] dx = { -1, 1, 0, 0 };
11+
private static final int[] dy = { 0, 0, -1, 1 };
12+
private static final int[][][] dir = {
13+
{ { 0 } },
14+
{ { 0 }, { 1 }, { 2 }, { 3 } }, // 1번 CCTV
15+
{ { 0, 1 }, { 2, 3 } }, // 2번
16+
{ { 0, 2 }, { 0, 3 }, { 1, 2 }, { 1, 3 } },
17+
{ { 0, 1, 2 }, { 0, 1, 3 }, { 1, 2, 3 }, { 0, 2, 3 } },
18+
{ { 0, 1, 2, 3 } }
19+
};
20+
21+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
22+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
23+
private static StringTokenizer st;
24+
25+
private static int N, M, res;
26+
private static int[][] map;
27+
28+
private static int cctvCnt;
29+
private static List<CCTV> cctvs = new ArrayList<>();
30+
31+
static class CCTV {
32+
int x;
33+
int y;
34+
int type;
35+
36+
public CCTV(int x, int y, int type) {
37+
this.x = x;
38+
this.y = y;
39+
this.type = type;
40+
}
41+
}
42+
43+
public static void main(String[] args) throws IOException {
44+
init();
45+
sol(0, map);
46+
47+
bw.write(res + "");
48+
bw.flush();
49+
bw.close();
50+
br.close();
51+
}
52+
53+
public static void init() throws IOException {
54+
st = new StringTokenizer(br.readLine());
55+
N = Integer.parseInt(st.nextToken());
56+
M = Integer.parseInt(st.nextToken());
57+
58+
res = Integer.MAX_VALUE;
59+
map = new int[N][M];
60+
61+
for (int i = 0; i < N; i++) {
62+
st = new StringTokenizer(br.readLine());
63+
64+
for (int j = 0; j < M; j++) {
65+
map[i][j] = Integer.parseInt(st.nextToken());
66+
67+
if (1 <= map[i][j] && map[i][j] <= 5) {
68+
cctvs.add(new CCTV(i, j, map[i][j]));
69+
}
70+
}
71+
}
72+
73+
cctvCnt = cctvs.size();
74+
}
75+
76+
public static void sol(int depth, int[][] prev) {
77+
if (depth == cctvCnt) {
78+
res = Math.min(res, countBlind(prev));
79+
return;
80+
}
81+
82+
int[][] copied = new int[N][M];
83+
84+
CCTV cur = cctvs.get(depth);
85+
int type = cur.type;
86+
int rotate = dir[type].length;
87+
88+
for (int i = 0; i < rotate; i++) {
89+
copy(copied, prev);
90+
91+
for (int j = 0; j < dir[type][i].length; j++) {
92+
countWatched(cur.x, cur.y, dir[type][i][j], copied);
93+
}
94+
95+
sol(depth + 1, copied);
96+
}
97+
}
98+
99+
public static int countBlind(int[][] arr) {
100+
int blind = 0;
101+
for (int i = 0; i < N; i++) {
102+
for (int j = 0; j < M; j++) {
103+
if (arr[i][j] == 0) {
104+
blind++;
105+
}
106+
}
107+
}
108+
return blind;
109+
}
110+
111+
public static int countWatched(int x, int y, int dir, int[][] copied) {
112+
int cnt = 0;
113+
while (true) {
114+
x += dx[dir];
115+
y += dy[dir];
116+
117+
if (OOB(x, y) || copied[x][y] == 6) {
118+
return cnt;
119+
}
120+
121+
if (copied[x][y] == 0) {
122+
cnt++;
123+
copied[x][y] = -1;
124+
}
125+
}
126+
}
127+
128+
public static void copy(int[][] from, int[][] to) {
129+
for (int i = 0; i < N; i++) {
130+
from[i] = Arrays.copyOf(to[i], M);
131+
}
132+
}
133+
134+
private static boolean OOB(int x, int y) {
135+
return x < 0 || N <= x || y < 0 || M <= y;
136+
}
137+
}
138+
```

0 commit comments

Comments
 (0)