[C++] 14502: 연구소

쩡우·2023년 1월 15일
0

BOJ algorithm

목록 보기
34/65

문제

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

연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.

일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다. 새로 세울 수 있는 벽의 개수는 3개이며, 꼭 3개를 세워야 한다.

예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자.

2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

이때, 0은 빈 칸, 1은 벽, 2는 바이러스가 있는 곳이다. 아무런 벽을 세우지 않는다면, 바이러스는 모든 빈 칸으로 퍼져나갈 수 있다.

2행 1열, 1행 2열, 4행 6열에 벽을 세운다면 지도의 모양은 아래와 같아지게 된다.

2 1 0 0 1 1 0
1 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 1 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

바이러스가 퍼진 뒤의 모습은 아래와 같아진다.

2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0

벽을 3개 세운 뒤, 바이러스가 퍼질 수 없는 곳을 안전 영역이라고 한다. 위의 지도에서 안전 영역의 크기는 27이다.

연구소의 지도가 주어졌을 때 얻을 수 있는 안전 영역 크기의 최댓값을 구하는 프로그램을 작성하시오.

입력

첫째 줄에 지도의 세로 크기 N과 가로 크기 M이 주어진다. (3 ≤ N, M ≤ 8)

둘째 줄부터 N개의 줄에 지도의 모양이 주어진다. 0은 빈 칸, 1은 벽, 2는 바이러스가 있는 위치이다. 2의 개수는 2보다 크거나 같고, 10보다 작거나 같은 자연수이다.

빈 칸의 개수는 3개 이상이다.

출력

첫째 줄에 얻을 수 있는 안전 영역의 최대 크기를 출력한다.

풀이

브루트포스 + BFS 문제!

지도의 최대 크기가 8 x 8이고, 새로 세우는 벽이 3개여야만 하기 때문에, 지도의 빈 곳에 벽 3개를 세울 수 있는 경우를 모두 계산하여 안전 영역의 최대값을 구하였다.

빈 칸인 0의 좌표를 모두 vector에 넣어 놓고 그 좌표 3개로 만들 수 있는 모든 조합을 구해서 3개의 0을 1로 바꿔주었다. 그 후에 BFS로 바이러스가 퍼진 후에 0의 개수(안전영역 개수)를 구하였다.

코드

#include <iostream>
#include <queue>

using namespace std;

void input_data(void);
void find_maximum(void);
int safety_count(int wall_1, int wall_2, int wall_3);

int n, m, result;
int original_map[8][8];
int copy_map[8][8];
vector<pair<int, int>> empty_coordinate;
vector<pair<int, int>> virus_coordinate;
queue<pair<int, int>> bfs_queue;
int move_x[4] = {-1, 1, 0, 0};
int move_y[4] = {0, 0, -1, 1};

int main(void)
{
	input_data();
	find_maximum();

	return (0);
}

void input_data(void)
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> m;

	int i = -1;
	while (++i < n)
	{
		int j = -1;
		while (++j < m)
		{
			cin >> original_map[i][j];
			if (original_map[i][j] == 0)
				empty_coordinate.push_back({i, j});
			else if (original_map[i][j] == 2)
				virus_coordinate.push_back({i, j});
		}
	}

	return ;
}

void find_maximum(void)
{
	int empty_size = empty_coordinate.size();
	int wall_1 = -1;
	while (++wall_1 < empty_size - 2)
	{
		int wall_2 = wall_1;
		while (++wall_2 < empty_size - 1)
		{
			int wall_3 = wall_2;
			while (++wall_3 < empty_size)
				result = max(result, safety_count(wall_1, wall_2, wall_3));
		}
	}

	cout << result << '\n';

	return ;
}

int safety_count(int wall_1, int wall_2, int wall_3)
{
	copy(&original_map[0][0], &original_map[0][0] + 64, &copy_map[0][0]);
	copy_map[empty_coordinate[wall_1].first][empty_coordinate[wall_1].second] = 1;
	copy_map[empty_coordinate[wall_2].first][empty_coordinate[wall_2].second] = 1;
	copy_map[empty_coordinate[wall_3].first][empty_coordinate[wall_3].second] = 1;

	int i = -1;
	while (++i < virus_coordinate.size())
		bfs_queue.push(virus_coordinate[i]);

	while (!bfs_queue.empty())
	{
		int now_x = bfs_queue.front().second;
		int now_y = bfs_queue.front().first;
		bfs_queue.pop();

		i = -1;
		while (++i < 4)
		{
			int next_x = now_x + move_x[i];
			int next_y = now_y + move_y[i];
			if (next_x >= 0 && next_x < m && next_y >= 0 && next_y < n && copy_map[next_y][next_x] == 0)
			{
				bfs_queue.push({next_y, next_x});
				copy_map[next_y][next_x] = 2;
			}
		}	
	}

	int count = 0;
	i = -1;
	while (++i < n)
	{
		int j = -1;
		while (++j < m)
			if (copy_map[i][j] == 0)
				count++;
	}

	return (count);
}

성공 !

profile
Jeongwoo's develop story

0개의 댓글