[BOJ] 15683 감시

Time Lapse :53min 32sec

15683.c

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
# pragma optimize("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
# pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <stdio.h>
#define gc() getchar_unlocked()
int map[8][8], N, M;
int cctv[9][4], cctvs=0, ans = 987654321;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
int fRI(){
register int N = gc(), r = 0;
for(;0x30>N||N>0x3A;N=gc());
do{
r = (r<<3) + (r<<1) + (N&0b1111); N = gc();
}while(0x30<=N);
return r;
}
void run(int f){
register int ny, nx;
for(int i = 0; i < cctvs; ++i){
if(cctv[i][2]==5) continue;
switch(cctv[i][2]){
case 1:
nx = cctv[i][1] + dx[cctv[i][3]];
ny = cctv[i][0] + dy[cctv[i][3]];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[cctv[i][3]];
nx += dx[cctv[i][3]];
}
break;
case 2:
nx = cctv[i][1] + dx[cctv[i][3]];
ny = cctv[i][0] + dy[cctv[i][3]];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[cctv[i][3]];
nx += dx[cctv[i][3]];
}
nx = cctv[i][1] + dx[(cctv[i][3]+2)];
ny = cctv[i][0] + dy[(cctv[i][3]+2)];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[cctv[i][3]+2];
nx += dx[cctv[i][3]+2];
}
break;
case 3:
nx = cctv[i][1] + dx[cctv[i][3]];
ny = cctv[i][0] + dy[cctv[i][3]];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[cctv[i][3]];
nx += dx[cctv[i][3]];
}
nx = cctv[i][1] + dx[(cctv[i][3]+1)%4];
ny = cctv[i][0] + dy[(cctv[i][3]+1)%4];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[(cctv[i][3]+1)%4];
nx += dx[(cctv[i][3]+1)%4];
}
break;
case 4:
nx = cctv[i][1] + dx[cctv[i][3]];
ny = cctv[i][0] + dy[cctv[i][3]];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[cctv[i][3]];
nx += dx[cctv[i][3]];
}
nx = cctv[i][1] + dx[(cctv[i][3]+1)%4];
ny = cctv[i][0] + dy[(cctv[i][3]+1)%4];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[(cctv[i][3]+1)%4];
nx += dx[(cctv[i][3]+1)%4];
}
nx = cctv[i][1] + dx[(cctv[i][3]+2)%4];
ny = cctv[i][0] + dy[(cctv[i][3]+2)%4];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
f ? ++map[ny][nx] : --map[ny][nx];
ny += dy[(cctv[i][3]+2)%4];
nx += dx[(cctv[i][3]+2)%4];
}
break;
default: break;
}
}
}
void DFS(int cur){
if(cur==cctvs){
run(1);
int sagak = 0;
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
if(!map[i][j]) ++sagak;
ans = ans > sagak ? sagak : ans;
run(0);
return;
}
if(cctv[cur][2]==5) {DFS(cur+1); return;}
if(cctv[cur][2]==2) {
cctv[cur][3]=0;
DFS(cur+1);
cctv[cur][3]=1;
DFS(cur+1);
return;
}
for(int i = 0; i < 4; ++i){
cctv[cur][3] = i;
DFS(cur+1);
}
}
int main(void){
register int i, j;
N = fRI(), M = fRI();
for(i = 0; i < N; ++i)
for(j = 0; j < M; ++j) {
map[i][j] = fRI();
if(map[i][j]&&map[i][j]!=6){
cctv[cctvs][0] = i;
cctv[cctvs][1] = j;
cctv[cctvs++][2] = map[i][j];
}
}
for(i = 0; i < cctvs; ++i)
if(cctv[i][2]==5){
int nx, ny;
for(j=0;j<4;j++){
nx = cctv[i][1] + dx[j];
ny = cctv[i][0] + dy[j];
while(0<=nx&&nx<M && 0<=ny&&ny<N && map[ny][nx]!=6){
++map[ny][nx];
ny += dy[j];
nx += dx[j];
}
}
}
DFS(0);
printf("%d",ans);
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}

15683.java

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
237
238
239
240
241
242
243
244
import java.io.*;

public class Main {
static int[][] map = new int[8][8];
static int[][] v = new int[8][8];
static int[] dx = {0, 1, 0, -1};
static int[] dy = {-1, 0, 1, 0};
static cctv[] cctvs = new cctv[8];
static int n, m, cnt, ans = 987654321;
public static void main(String[] args) throws IOException {
Reader r = new Reader();
n = r.nextInt();
m = r.nextInt();
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j) {
map[i][j] = r.nextInt();
if(1 <= map[i][j] && map[i][j] <= 5)
cctvs[cnt++] = new cctv(i, j);
}
for(int i = 0; i < cnt; ++i)
if(map[cctvs[i].f][cctvs[i].s] == 5)
for(int k = 0; k < 4; ++k) {
int nx = cctvs[i].s + dx[k];
int ny = cctvs[i].f + dy[k];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[k];
nx += dx[k];
}
}

bfs(0);
System.out.println(ans);
}

static void bfs(int idx) {
if(idx == cnt) {
int ret = 0;
for(int i = 0; i < n && ret < ans && ans != 0; ++i)
for(int j = 0; j < m; ++j)
if(v[i][j] == 0 && map[i][j] == 0)
++ret;
ans = Math.min(ans, ret);
return;
}
switch (map[cctvs[idx].f][cctvs[idx].s]) {
case 1:
for(int d = 0; d < 4; ++d) {
int nx = cctvs[idx].s + dx[d];
int ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[d];
nx += dx[d];
}
bfs(idx + 1);
nx = cctvs[idx].s + dx[d];
ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[d];
nx += dx[d];
}
}
break;
case 2:
for(int d = 0; d < 2; ++d) {
int nx = cctvs[idx].s + dx[d];
int ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[d];
nx += dx[d];
}
nx = cctvs[idx].s + dx[d + 2];
ny = cctvs[idx].f + dy[d + 2];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[d + 2];
nx += dx[d + 2];
}
bfs(idx + 1);
nx = cctvs[idx].s + dx[d];
ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[d];
nx += dx[d];
}

nx = cctvs[idx].s + dx[d + 2];
ny = cctvs[idx].f + dy[d + 2];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[d + 2];
nx += dx[d + 2];
}
}
break;
case 3:
for(int d = 0; d < 4; ++d) {
int nx = cctvs[idx].s + dx[d];
int ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[d];
nx += dx[d];
}
nx = cctvs[idx].s + dx[(d + 1) % 4];
ny = cctvs[idx].f + dy[(d + 1) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[(d + 1) % 4];
nx += dx[(d + 1) % 4];
}
bfs(idx + 1);
nx = cctvs[idx].s + dx[d];
ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[d];
nx += dx[d];
}
nx = cctvs[idx].s + dx[(d + 1) % 4];
ny = cctvs[idx].f + dy[(d + 1) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[(d + 1) % 4];
nx += dx[(d + 1) % 4];
}
}
break;
case 4:
for(int d = 0; d < 4; ++d) {
int nx = cctvs[idx].s + dx[d];
int ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[d];
nx += dx[d];
}
nx = cctvs[idx].s + dx[(d + 1) % 4];
ny = cctvs[idx].f + dy[(d + 1) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[(d + 1) % 4];
nx += dx[(d + 1) % 4];
}

nx = cctvs[idx].s + dx[(d + 3) % 4];
ny = cctvs[idx].f + dy[(d + 3) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
++v[ny][nx];
ny += dy[(d + 3) % 4];
nx += dx[(d + 3) % 4];
}
bfs(idx + 1);
nx = cctvs[idx].s + dx[d];
ny = cctvs[idx].f + dy[d];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[d];
nx += dx[d];
}
nx = cctvs[idx].s + dx[(d + 1) % 4];
ny = cctvs[idx].f + dy[(d + 1) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[(d + 1) % 4];
nx += dx[(d + 1) % 4];
}
nx = cctvs[idx].s + dx[(d + 3) % 4];
ny = cctvs[idx].f + dy[(d + 3) % 4];
while(0 <= nx && nx < m && 0 <= ny && ny < n && map[ny][nx] != 6) {
--v[ny][nx];
ny += dy[(d + 3) % 4];
nx += dx[(d + 3) % 4];
}
}
break;
case 5: bfs(idx + 1);
default: break;
}
}

static class cctv{
public int f, s;
public cctv(int f, int s) {
this.f = f;
this.s = s;
}
}

static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;

public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public int nextInt() throws IOException {
int ret = 0, flag = 1;
byte c = read();
if(c == '-') {
flag = -1;
c = read();
}
do {
ret = (ret<<3) + (ret<<1) + (c & 0b1111);
} while ((c = read()) >= '0' && c <= '9');

return ret * flag;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}


private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}

private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/15683/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.