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
- cs
- DP
- 코딩
- Computer science
- 브루트포스
- 개발
- 백준
- 오에스
- 컴공과
- 정석학술정보관
- 문제풀이
- 구현
- Stack
- Operating System
- 그래프
- bfs
- 자료구조
- 컴공
- c++
- 오퍼레이팅시스템
- coding
- 북리뷰
- 정석
- 컴퓨터공학과
- 알고리즘
- 코테
- OS
- 스택
- vector
- 너비우선탐색
Archives
- Today
- Total
Little Jay
[C++]백준 14502 - 연구소 본문
#include <iostream>
#include <algorithm>
#include <queue>
#define MAX 8
using namespace std;
int n, m;
int virus[MAX][MAX];
int temp[MAX][MAX];
int dx[] = { 1, -1, 0, 0 };
int dy[] = { 0, 0, -1, 1 };
int bfs() {
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
temp[i][j] = virus[i][j];
if (temp[i][j] == 2)
q.push(make_pair(i, j));
}
}
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
if (temp[nx][ny] == 0) {
temp[nx][ny] = 2;
q.push(make_pair(nx, ny));
}
}
}
}
int safe_zone = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (temp[i][j] == 0)
safe_zone++;
}
}
return safe_zone;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> virus[i][j];
}
}
int max_safe = 0;
//여기 벽 세우는 부분은 외부 코드 참조함......
for (int w1 = 0; w1 < n; w1++) {
for (int w2 = 0; w2 < m; w2++) {
if (virus[w1][w2] != 0) {
continue;
}
for (int q1 = 0; q1 < n; q1++) {
for (int q2 = 0; q2 < m; q2++) {
if (virus[q1][q2] != 0)
continue;
for (int e1 = 0; e1 < n; e1++) {
for (int e2 = 0; e2 < m; e2++) {
if (virus[e1][e2] != 0)
continue;
if (w1 == q1 && w2 == q2)
continue;
if (w1 == e1 && w2 == e2)
continue;
if (q1 == e1 && q2 == e2)
continue;
virus[w1][w2] = 1;
virus[q1][q2] = 1;
virus[e1][e2] = 1;
int safe = bfs();
if (safe > max_safe)
max_safe = safe;
virus[w1][w2] = 0;
virus[q1][q2] = 0;
virus[e1][e2] = 0;
}
}
}
}
}
}
cout << max_safe << '\n';
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1010 - 다리놓기 (0) | 2021.09.20 |
---|---|
[C++] 백준 16768번 - Mooyo Mooyo (0) | 2021.09.20 |
[C++] 백준 15683번 - 감시 (0) | 2021.09.18 |
[C++] 백준 2941번 - 크로아티아 알파벳 (0) | 2021.09.07 |
[C++] 백준 1065번 - 한수 (0) | 2021.09.05 |
Comments