1. CCTV정보를 담을 구조체를 만든다.
2. CCTV들을 벡터에 넣는다.
3. CCTV들이 감시할 수 있는 영역에 대하여 완전탐색한다.
나는 check 함수를 이용하여 사각지대 여부를 확인하였다.
단순히 bool 형으로 true false 로 설정하면 중복되는 부분이 지워지는 경우가 있어서
int형으로 만들고 ++, -- 해주며 사각지대를 찾았다.
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 | #include <iostream> #include <algorithm> #include <vector> using namespace std; struct Node { int x; int y; int num; Node(int x,int y,int n):x(x),y(y),num(n){} }; int n, m, res; int map[8][8]; int visited[8][8]; vector<Node> v; //dir 0북 1동 2남 3서 void check(int x,int y, int dir,int flag) { if (dir == 0) { --x; while (x >= 0) { if (map[x][y] == 6)return; if (flag == 0) visited[x][y]++; else visited[x][y]--; x--; } } else if (dir == 1) { ++y; while (y < m) { if (map[x][y] == 6)return; if (flag == 0) visited[x][y]++; else visited[x][y]--; y++; } } else if (dir == 2) { ++x; while (x < n) { if (map[x][y] == 6)return; if (flag == 0) visited[x][y]++; else visited[x][y]--; x++; } } else { --y; while (y >= 0) { if (map[x][y] == 6)return; if (flag == 0) visited[x][y]++; else visited[x][y]--; y--; } } } int get_result() { int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (map[i][j]==0 && visited[i][j]==0) ans++; } } return ans; } void printAll() { cout << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (visited[i][j]) cout << "# "; else cout <<map[i][j]<<" "; } cout << endl; } } void dfs(int idx) { if (idx == v.size()) { res = min(res, get_result()); //printAll(); return; } int x = v[idx].x; int y = v[idx].y; int num = v[idx].num; if (num == 1) { //4방향 for (int i = 0; i < 4; i++) { check(x, y, i, 0); dfs(idx + 1); check(x, y, i, 1); } } else if (num == 2) { for (int i = 0; i < 2; i++) { check(x, y, i, 0); check(x, y, i + 2, 0); dfs(idx + 1); check(x, y, i, 1); check(x, y, i + 2, 1); } } else if (num == 3) { for (int i = 0; i < 4; i++) { check(x, y, i, 0); check(x, y, (i + 1 == 4) ? 0 : i + 1, 0); dfs(idx + 1); check(x, y, i, 1); check(x, y, (i + 1 == 4) ? 0 : i + 1, 1); } } else if (num == 4) { for (int i = 0; i < 4; i++) { check(x, y, i, 0); check(x, y, (i + 1 >= 4) ? i + 1 - 4 : i + 1, 0); check(x, y, (i + 2 >= 4) ? i + 2 - 4 : i + 2, 0); dfs(idx + 1); check(x, y, i, 1); check(x, y, (i + 1 >= 4) ? i + 1 - 4 : i + 1, 1); check(x, y, (i + 2 >= 4) ? i + 2 - 4 : i + 2, 1); } } else if (num == 5) { for (int i = 0; i < 4; i++)check(x, y, i, 0); dfs(idx + 1); for (int i = 0; i < 4; i++)check(x, y, i, 1); } } int main() { 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)continue; v.push_back(Node(i, j, map[i][j])); } } res = 987654321; dfs(0); cout << res << endl; } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
15684 사다리 조작 (0) | 2018.09.07 |
---|---|
2580 스도쿠 (0) | 2018.04.21 |
15686 드래곤 커브 (0) | 2018.04.16 |
15686 치킨 배달 (3) | 2018.04.16 |
1182 부분집합의 합 (4) | 2018.04.13 |