-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15683.cpp
128 lines (120 loc) · 2.41 KB
/
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
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
using namespace std;
int xxxx[6][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{
{-1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 0}
},
{
{-1, 0, 1, 0},
{0, 0, 0, 0},
{-1, 0, 1, 0},
{0, 0, 0, 0}
},
{
{-1, 0, 0, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
{-1, 0, 0, 0}
},
{
{-1, 0, 0, 0},
{-1, 0, 1, 0},
{0, 0, 1, 0},
{-1, 0, 1, 0}
},
{
{-1, 0, 1, 0},
{-1, 0, 1, 0},
{-1, 0, 1, 0},
{-1, 0, 1, 0}
}
};
int yyyy[6][4][4] = {
{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{
{0, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, -1}
},
{
{0, 0, 0, 0},
{0, 1, 0, -1},
{0, 0, 0, 0},
{0, 1, 0, -1}
},
{
{0, 1, 0, 0},
{0, 1, 0, 0},
{0, 0, 0, -1},
{0, 0, 0, -1}
},
{
{0, 1, 0, -1},
{0, 1, 0, 0},
{0, 1, 0, -1},
{0, 0, 0, -1}
},
{
{0, 1, 0, -1},
{0, 1, 0, -1},
{0, 1, 0, -1},
{0, 1, 0, -1}
}
};
int N, M;
int map[10][10];
vector<pair<int,int>> cctv;
int dfs(int x, int direction, bool temp_visited[10][10]) {
if (x == cctv.size()) {
int result = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (!temp_visited[i][j]) result++;
}
}
return result;
}
bool visited[10][10]; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) visited[i][j] = temp_visited[i][j];
int result = 9999;
for (int i = 0; i < 4; i++) {
auto [nx, ny] = cctv[x];
int type = map[nx][ny];
int px = xxxx[type][direction][i];
int py = yyyy[type][direction][i];
while (px || py) {
nx += px;
ny += py;
if (nx < 0 || nx >= N || ny < 0 || ny >= M || map[nx][ny] == 6) break;
visited[nx][ny] = 1;
}
}
for (int i = 0; i < 4; i++) {
result = min(result, dfs(x + 1, i, visited));
}
return result;
}
int main(){
cin.tie(0) -> sync_with_stdio(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] >= 1 && map[i][j] < 6) cctv.push_back({i, j});
}
}
int result = 9999;
for (int i = 0; i < 4; i++) {
bool visited[10][10]; for (int j = 0; j < N; j++) for (int k = 0; k < M; k++) visited[j][k] = map[j][k] != 0;
result = min(result, dfs(0, i, visited));
}
cout << result;
return 0;
}