728x90
14502 연구소
https://www.acmicpc.net/problem/14502
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크
www.acmicpc.net


#include<bits/stdc++.h>
using namespace std;
const int mxN = 8, dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 };
int n, m, a[mxN][mxN];
int tmp[mxN][mxN];
int ans = 0;
bool vis[mxN][mxN];
void copy(int tmp[mxN][mxN], int a[mxN][mxN]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
tmp[i][j] = a[i][j];
}
}
}
void bfs()
{
int after[8][8];
copy(after, tmp);
queue<pair<int, int>> q;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (after[i][j] == 2)
q.push({ i,j });
}
}
while (q.size())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int k = 0; k < 4; ++k)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || nx >= n || ny < 0 || ny >= m)
continue;
if (after[nx][ny] == 0)
{
after[nx][ny] = 2;
q.push({ nx,ny });
}
}
}
int cnt = 0;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (after[i][j] == 0)
++cnt;
}
}
ans = max(ans, cnt);
}
void wall(int cur)
{
if (cur == 3)
{
bfs();
return;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (tmp[i][j] == 0)
{
tmp[i][j] = 1;
wall(cur + 1);
tmp[i][j] = 0;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> a[i][j];
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (a[i][j] == 0)
{
memset(vis, 0, sizeof(vis));
copy(tmp, a);
tmp[i][j] = 1;
wall(1);
tmp[i][j] = 0;
}
}
}
cout << ans;
}
- 바이러스가 상하좌우로 퍼지기 때문에 BFS를 사용했다.
- 벽을 3개 세울 수 있기 때문에 모든 빈칸중 벽을 건설해 안전 영역을 구해야한다.
- 원본 그래프를 복사하며 사용한다.
728x90
'Study > Baekjoon' 카테고리의 다른 글
[백준/C++] 11728 배열 합치기 (0) | 2024.04.08 |
---|---|
[백준/C++] 1269 대칭 차집합 (1) | 2024.04.06 |
[백준/C++] 1005 ACM Craft (0) | 2024.03.28 |
[백준/C++] 17130 토끼가 정보섬에 올라온 이유 (1) | 2024.03.27 |
[백준/C++] 1504 특정한 최단 경로 (0) | 2024.03.26 |