-
Notifications
You must be signed in to change notification settings - Fork 0
/
[BOJ] 15683 감시.cpp
157 lines (123 loc) · 3.21 KB
/
[BOJ] 15683 감시.cpp
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
#include <iostream>
#include <vector>
#define MAX 8
using namespace std;
struct Node{
int cctv; //cctv 종류
int y; //cctv y좌표
int x; //cctv x좌표
Node(int cctv,int y,int x) : cctv(cctv),y(y),x(x){};
};
int N, M;
int ans = 100;
int cctv_cnt = 0;
vector<vector<int>> map(MAX, vector<int>(MAX, 0));
vector<Node> v;
void move(int dir, int y, int x){
switch(dir){
//북
case 0:
for(int i = y-1; i >= 0; i--) {
if(map[i][x] == 6) break;
if(map[i][x] == 0) map[i][x] = -1; //cctv 감시 완료
}
break;
//동
case 1:
for(int j = x+1; j < M; j++) {
if(map[y][j] == 6) break;
if(map[y][j] == 0) map[y][j] = -1;
}
break;
//남
case 2:
for(int i=y+1;i<N;i++) {
if (map[i][x] == 6) break;
if (map[i][x] == 0) map[i][x] = -1;
}
break;
//서
case 3:
for(int j = x-1; j >= 0; j--) {
if(map[y][j] == 6) break;
if(map[y][j] == 0) map[y][j] = -1;
}
}
}
void DFS(int step) {
if(step == cctv_cnt){
int cnt=0;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(map[i][j] == 0)
cnt++;
}
}
ans = min(ans, cnt);
return;
}
int cctv = v[step].cctv;
int y = v[step].y;
int x = v[step].x;
//이전에 확인한 사무실 상태값 저장
vector<vector<int>> map2 = map;
switch(cctv){
case 1:
//북,동,남,서
for(int dir = 0; dir < 4; dir++){
move(dir, y, x);
DFS(step + 1);
map = map2;
}
break;
case 2:
//북남,동서
for(int dir = 0; dir < 2; dir++){
move(dir, y, x);
move(dir+2, y, x);
DFS(step + 1);
map = map2;
}
break;
case 3:
//북동,동남,남서,서북
for(int dir = 0; dir < 4; dir++){
move(dir, y, x);
move((dir+1)%4, y, x);
DFS(step + 1);
map = map2;
}
break;
case 4:
//북동남,동남서,남서북,서북동
for(int dir = 0; dir < 4; dir++){
move(dir, y, x);
move((dir+1)%4, y, x);
move((dir+2)%4, y, x);
DFS(step + 1);
map = map2;
}
break;
case 5:
for(int dir = 0; dir < 4; dir++)
move(dir, y, x);
DFS(step + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> map[i][j];
if (map[i][j] != 0 && map[i][j] != 6) {
cctv_cnt++;
v.push_back( Node(map[i][j], i, j) );
}
}
}
DFS(0);
cout << ans << endl;
return 0;
}