반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 오퍼레터
- OFFSET
- @P0
- 처우산정
- dfs
- 성적평가
- Kafka
- incr
- BFS
- 6987
- 백트래킹
- 처우협의
- softeer
- msSQL
- 물채우기
- 13908
- Docker
- 소프티어
- 이분탐색
- 파라메트릭
- boj #19237 #어른 상어
- compose
- 연결요소
- 경력
- 백준
- upper_bound
- 기술면접
- 매개변수탐색
- BOJ
- 퇴사통보
Archives
- Today
- Total
기술 블로그
2117번 홈 방범 서비스 본문
728x90
반응형
문제 : https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5V61LqAf8DFAWu
풀이 : https://www.swexpertacademy.com/main/learn/course/lectureHtmlViewer.do
마름모를 판단할 수 있으면, 쉬운 문제였으나, 마름모를 판단하지 못 한다면 어려운 문제다.
우선, 들어가기 전에 BFS는 마름모 모양으로 퍼져 나간다는 것을 기억하자.
아래의 코드(<BFS>)를 실행해 보면, 각각 <BFS 결과 1>과 <BFS 결과 2>처럼 나온다.
<BFS>
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 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; int map[26][26] = { 0, }; int s = 0, sy = 0, sx = 0; int dy[4] = { -1, 0, 1, 0 }; int dx[4] = { 0, -1, 0, 1 }; void printMap() { for (int i = 0; i < s; i++) { for (int j = 0; j < s; j++) { printf("%d", map[i][j]); } printf("\n"); } } void BFS(int r, int c) { queue<pair<int, int> > q; bool visit[26][26] = { false, }; q.push({ r,c }); map[r][c] = 1; visit[r][c] = true; int index = 1; while (!q.empty()) { int qSize = q.size(); while (qSize--) { int row = q.front().first; int column = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int y = row + dy[i]; int x = column + dx[i]; if (0 <= y && y < s && 0 <= x && x < s && !visit[y][x]) { visit[y][x] = true; q.push({ y,x }); map[y][x] = 1; } } } printf("%d번 째 BFS 결과 (아래)\n", index); printMap(); printf("\n"); ++index; if (index >= s) break; } } int main(void) { printf("마름모의 크기를 입력하시오. (최대 25) : "); scanf("%d", &s); printf("좌표를 입력하시오. (행, 열) : "); scanf("%d %d", &sy, &sx); BFS(sy, sx); return 0; } | cs |
<BFS 결과 1>
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 | 좌표를 입력하시오. (행, 열) : 2 2 1번 째 BFS 결과 (아래) 00000 00100 01110 00100 00000 2번 째 BFS 결과 (아래) 00100 01110 11111 01110 00100 3번 째 BFS 결과 (아래) 01110 11111 11111 11111 01110 4번 째 BFS 결과 (아래) 11111 11111 11111 11111 11111 | cs |
<BFS 결과 2>
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | 마름모의 크기를 입력하시오. (최대 25) : 20 좌표를 입력하시오. (행, 열) : 0 0 1번 째 BFS 결과 (아래) 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 2번 째 BFS 결과 (아래) 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 3번 째 BFS 결과 (아래) 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 4번 째 BFS 결과 (아래) 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 5번 째 BFS 결과 (아래) 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 6번 째 BFS 결과 (아래) 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 7번 째 BFS 결과 (아래) 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 8번 째 BFS 결과 (아래) 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 9번 째 BFS 결과 (아래) 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 10번 째 BFS 결과 (아래) 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 11번 째 BFS 결과 (아래) 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 12번 째 BFS 결과 (아래) 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 13번 째 BFS 결과 (아래) 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 14번 째 BFS 결과 (아래) 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 15번 째 BFS 결과 (아래) 11111111111111110000 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 16번 째 BFS 결과 (아래) 11111111111111111000 11111111111111110000 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 17번 째 BFS 결과 (아래) 11111111111111111100 11111111111111111000 11111111111111110000 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 00000000000000000000 18번 째 BFS 결과 (아래) 11111111111111111110 11111111111111111100 11111111111111111000 11111111111111110000 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 00000000000000000000 19번 째 BFS 결과 (아래) 11111111111111111111 11111111111111111110 11111111111111111100 11111111111111111000 11111111111111110000 11111111111111100000 11111111111111000000 11111111111110000000 11111111111100000000 11111111111000000000 11111111110000000000 11111111100000000000 11111111000000000000 11111110000000000000 11111100000000000000 11111000000000000000 11110000000000000000 11100000000000000000 11000000000000000000 10000000000000000000 | cs |
<정답 코드>
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 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; int map[26][26] = { 0, }; int sy = 0, sx = 0; int dy[4] = { -1, 0, 1, 0 }; int dx[4] = { 0, -1, 0, 1 }; int N = 0, M = 0; int max_houseCount = 0; void BFS(int r, int c) { queue<pair<int, int> > q; bool visit[26][26] = { false, }; q.push({ r,c }); visit[r][c] = true; int cnt = map[r][c]; // 집 표시를 1로 해주었으므로, 이것을 단순 '갯수'로 표현할 수 있다. while (!q.empty()) // queue가 비어있으면, 안 된다. { for (int K = 1; K <= 2 * N; K++) { int qSize = q.size(); int price = M*cnt - (K*K + (K - 1)*(K - 1)); // price = (보안회사의 이익) if (price >= 0 && max_houseCount < cnt) { // 보안회사가 손해를 보지 않고 = price가 음수'만' 아니면 된다. // 서비스 가능한 최대 집의 수 = BFS를 통해 마름모 모양으로 퍼져 나가면서 // K의 범위에 따라 속하는 집의 최대 수 max_houseCount = cnt; } while (qSize--) { int row = q.front().first; int column = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int y = row + dy[i]; int x = column + dx[i]; if (0 <= y && y < N && 0 <= x && x < N && !visit[y][x]) { visit[y][x] = true; q.push({ y,x }); if (map[y][x] == 1) ++cnt; // 집이면 cnt 1 증가 } } } } } } int main(void) { int T = 0; scanf("%d", &T); for (int tc = 1; tc <= T; tc++) { scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &map[i][j]); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { BFS(i, j); } } printf("#%d %d\n", tc, max_houseCount); max_houseCount = 0; } return 0; } | cs |
728x90
반응형
'알고리즘 문제 > SW Expert Academy' 카테고리의 다른 글
5653번 줄기세포 배양 (2) | 2018.10.05 |
---|---|
2477번 차량 정비소 (0) | 2018.09.14 |
2105번 디저트 카페 (0) | 2018.08.31 |
2382번 미생물 격리 (0) | 2018.08.29 |
1952번 수영장 (0) | 2018.08.27 |