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
| #include <iostream> #include <vector> using namespace std; int n, m, wall_cnt, ans; int room[9][9]; int selected[9]; int dx[4] = { -1, 1, 0, 0 }; int dy[4] = { 0, 0, -1, 1 }; int type_two[2][2] = {{0, 1}, {2, 3}}; int type_three[4][2] = { {0, 3}, {1, 3}, {0, 2}, {1, 2} }; int type_four[4][3] = { {2, 0, 3}, {0, 3, 1}, {2, 1, 3}, {0, 2, 1} }; struct INFO { int x, y, type; }; vector<INFO> cctv; void init() { for (int i = 0; i < 9; ++i) selected[i] = -1; ans = 1e9; wall_cnt = 0; } bool isBound(int x, int y) { if (x > -1 && y > -1 && x < n && y < m) return true; return false; } void task() { bool check[9][9] = { 0, }; int type, res = 0; for (int i = 0; i < cctv.size(); ++i) { int dir = selected[i]; int x = cctv[i].x, y = cctv[i].y; int d_x, d_y; if (cctv[i].type == 1) { d_x = x + dx[dir]; d_y = y + dy[dir]; while (isBound(d_x, d_y) && room[d_x][d_y] != 6) { if (!check[d_x][d_y] && room[d_x][d_y] == 0) { check[d_x][d_y] = true; res++; } d_x += dx[dir]; d_y += dy[dir]; } } else if (cctv[i].type == 2) { for (int j = 0; j < 2; ++j) { d_x = x + dx[type_two[dir][j]]; d_y = y + dy[type_two[dir][j]]; while (isBound(d_x, d_y) && room[d_x][d_y] != 6) { if (!check[d_x][d_y] && room[d_x][d_y] == 0) { check[d_x][d_y] = true; res++; } d_x += dx[type_two[dir][j]]; d_y += dy[type_two[dir][j]]; } } } else if (cctv[i].type == 3) { for (int j = 0; j < 2; ++j) { d_x = x + dx[type_three[dir][j]]; d_y = y + dy[type_three[dir][j]]; while (isBound(d_x, d_y) && room[d_x][d_y] != 6) { if (!check[d_x][d_y] && room[d_x][d_y] == 0) { check[d_x][d_y] = true; res++; } d_x += dx[type_three[dir][j]]; d_y += dy[type_three[dir][j]]; } } } else if (cctv[i].type == 4) { for (int j = 0; j < 3; ++j) { d_x = x + dx[type_four[dir][j]]; d_y = y + dy[type_four[dir][j]]; while (isBound(d_x, d_y) && room[d_x][d_y] != 6) { if (!check[d_x][d_y] && room[d_x][d_y] == 0) { check[d_x][d_y] = true; res++; } d_x += dx[type_four[dir][j]]; d_y += dy[type_four[dir][j]]; } } } else if (cctv[i].type == 5) { for (int j = 0; j < 4; ++j) { d_x = x + dx[j]; d_y = y + dy[j]; while (isBound(d_x, d_y) && room[d_x][d_y] != 6) { if (!check[d_x][d_y] && room[d_x][d_y] == 0) { check[d_x][d_y] = true; res++; } d_x += dx[j]; d_y += dy[j]; } } } } res = (n * m) - res - wall_cnt - cctv.size(); if (res < ans) ans = res; } void selectDirection(int idx, int cnt) { if (cnt == cctv.size()) { task(); return; } int type = cctv[idx].type; for (int i = 0; i < 4; ++i) { if (type == 2) { if (i == 2) break; } if(type == 5) { if (i == 1) break; } selected[idx] = i; selectDirection(idx + 1, cnt + 1); selected[idx] = -1; } } int main() { cin >> n >> m; init(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> room[i][j]; if (room[i][j] >= 1 && room[i][j] <= 5) { cctv.push_back({ i, j, room[i][j] }); } else if (room[i][j] == 6) wall_cnt++; } } selectDirection(0, 0); cout << ans << "\n"; return 0; }
|