Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- migration
- eslint
- Effective Java
- expire_logs_days
- java
- REACTJS
- Spring Batch
- regex
- REACT
- spring
- Effective Java 3/e
- git
- 퀵소트
- MySQL
- mysql 5.5
- Chunk
- current_date
- JavaScript
- Node
- spring cloud
- Express
- upgrade
- Regular expression
- nodejs
- update
- log_bin
- Webpack
- npm
- 정규표현식
- log4j2
Archives
- Today
- Total
내 세상
[Algorithm] 백준 14502번 연구소 본문
728x90
반응형
https://www.acmicpc.net/problem/14502
백준 14502번 연구소
이거는 그냥 맵 크기가 작기 때문에 전부 다 찾고, 전부 다 돌려보면 됨.
흔히 말하는 완전탐색
자기 전에 빠르게 짜보기 위해 엄청 대충 짰기 때문에 다시 한번 정신이 말짱할 때 깔끔하게
그리고 변수 크기가 커져도 제한 시간 내에 작동할 수 있도록 수정을 해봐야 겠다.
문제 풀이
1. 벽을 "무조건" 3개 설치 해야함.
2. 바이러스는 벽을 뚫지 못함.
3. 바이러스가 퍼지지 않은 안전 지대의 영역 크기를 구해야 함.
→ 전체적으로 맵 크기가 작기 때문에, for문 돌면서 간단하게 풀어도 잘 풀림. (추후 수정 예정)
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 | #include <iostream> #include <vector> using namespace std; int N, M, result = -1; int gCnt, oCnt; vector < vector < int > > map; vector < vector < int > > visit; vector < vector < int > > visitOrigin; vector < vector < int > > temp; int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; void polluteProcess(int x, int y) { if (gCnt <= 0) return; if (gCnt <= result) return; for (int i = 0; i < 4; i++) { int cx = x + dx[i]; int cy = y + dy[i]; if (cx >= 0 && cy >= 0 && cx < N && cy < M) { if (map[cx][cy] != 1 && visit[cx][cy] == 0) { map[cx][cy] = 2; visit[cx][cy] = 1; gCnt--; polluteProcess(cx, cy); } } } } void pollute() { visitOrigin = visit; temp = map; gCnt = oCnt - 3; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (map[i][j] == 2) { polluteProcess(i, j); } } } if (result < gCnt) result = gCnt; map = temp; visit = visitOrigin; } void dfs(int x, int cnt) { if (cnt == 3) { pollute(); return; } for (int i = x; i < N; i++) { for (int j = 0; j < M; j++) { if (map[i][j] == 0) { map[i][j] = 1; visit[i][j] = 1; dfs(i, cnt + 1); map[i][j] = 0; visit[i][j] = 0; } } } } int main() { cin >> N >> M; map.assign(N + 1, vector<int>(M + 1, 0)); visit.assign(N + 1, vector<int>(M + 1, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> map[i][j]; if (map[i][j] != 0) { visit[i][j] = 1; } else { gCnt++; } } } oCnt = gCnt; dfs(0,0); cout << result; return 0; } | cs |
728x90
반응형
'Coding > Algorithms' 카테고리의 다른 글
[Algorithm] 백준 15686번 치킨 배달 (0) | 2019.03.17 |
---|---|
[Algorithm] 백준 5373번 큐빙 (0) | 2019.03.10 |
[Algorithm] 백준 10951번 A+B-4 (0) | 2019.02.21 |
[Algorithm] 백준 16235번 나무 재테크 (0) | 2019.02.21 |
[Algorithm] 백준 16236번 아기 상어 (0) | 2019.02.18 |