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
| #include <iostream>
#include <cstring>
#define MAX_WIDTH 8
#define WALL 6
#define BLINDSPOT 0
using namespace std;
typedef struct CCTV {
int row;
int col;
int direction; // 1 : 북, 2 : 동, 3 : 남, 4 : 서
int num;
} CCTV;
int height, width;
int map[MAX_WIDTH][MAX_WIDTH];
int tempMap[MAX_WIDTH][MAX_WIDTH];
CCTV cctv[MAX_WIDTH];
int numOfCctv;
int minBlindSpot(CCTV c[MAX_WIDTH], int len = 0);
int calcBlindSpot(CCTV c[MAX_WIDTH]);
void up(int i, int j);
void right(int i, int j);
void down(int i, int j);
void left(int i, int j);
int main() {
cin >> height >> width;
int index = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
int temp;
cin >> temp;
map[i][j] = temp;
if (temp != 0 && temp != 6) {
cctv[index].row = i;
cctv[index].col = j;
cctv[index].direction = 0;
cctv[index++].num = temp;
}
}
numOfCctv = index;
cout << minBlindSpot(cctv);
}
int minBlindSpot(CCTV c[MAX_WIDTH], int len) {
if (len == numOfCctv)
return calcBlindSpot(c);
int temp[4];
int last = c[len].num != 1 ? c[len].num != 2 ? c[len].num != 3 ? c[len].num != 4 ? 1 : 4 : 4 : 2 : 4;
for (int i = 1; i <= last; i++) {
c[len].direction = i;
temp[i-1] = minBlindSpot(c, len + 1);
}
int min = temp[0];
for (int i = 1; i < last; i++)
min = temp[i] < min ? temp[i] : min;
return min;
}
int calcBlindSpot(CCTV c[MAX_WIDTH]) {
for (int i = 0; i < height; i++)
memcpy(tempMap[i], map[i], sizeof(int) * width);
for (int i = 0; i < numOfCctv; i++) {
if (c[i].num == 5) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].num == 2) {
if (c[i].direction == 1) {
right(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
up(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
}
else {
if (c[i].num == 1) {
if (c[i].direction == 1)
up(c[i].row, c[i].col);
else if (c[i].direction == 2)
right(c[i].row, c[i].col);
else if (c[i].direction == 3)
down(c[i].row, c[i].col);
else if (c[i].direction == 4)
left(c[i].row, c[i].col);
}
else if (c[i].num == 3) {
if (c[i].direction == 1) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 3) {
down(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 4) {
left(c[i].row, c[i].col);
up(c[i].row, c[i].col);
}
}
else if (c[i].num == 4) {
if (c[i].direction == 1) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
left(c[i].row, c[i].col);
}
else if (c[i].direction == 2) {
up(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 3) {
left(c[i].row, c[i].col);
right(c[i].row, c[i].col);
down(c[i].row, c[i].col);
}
else if (c[i].direction == 4) {
left(c[i].row, c[i].col);
down(c[i].row, c[i].col);
up(c[i].row, c[i].col);
}
}
}
}
int count = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if (tempMap[i][j] == 0)
count++;
return count;
}
void up(int i, int j) {
for (int k = i - 1; k >= 0; k--) {
if (tempMap[k][j] != 6)
tempMap[k][j] = -1;
else
break;
}
}
void right(int i, int j) {
for (int k = j + 1; k < width; k++) {
if (tempMap[i][k] != 6)
tempMap[i][k] = -1;
else
break;
}
}
void down(int i, int j) {
for (int k = i+ 1; k < height; k++) {
if (tempMap[k][j] != 6)
tempMap[k][j] = -1;
else
break;
}
}
void left(int i, int j) {
for (int k = j - 1; k >= 0; k--) {
if (tempMap[i][k] != 6)
tempMap[i][k] = -1;
else
break;
}
}
|