백준/완전 탐색

[백준 14502번 / C++] 연구소

배발자 2022. 1. 28.
반응형

1. 벽을 설치 할 수 있는 경우를 완전탐색으로 구하기 

2. 벽을 설치 할 수 있는 경우에 실제 map에 벽으로 만들기 

3. bfs 돌리기

4. 안전지역인 영역이 큰 값으로 갱신해나가기

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
#include <algorithm>
using namespace std; 
int map[8][8]; 
int wall[8][8]; 
int dx[4] = { 0,0,1,-1 }; 
int dy[4] = { 1,-1,0,0 }; 
int max_cnt = -1; 
int m, n; 
int path_cnt; 
queue < pair<int, int>>q;
void bfs() {

	int s_map[8][8]; 
	int visited[8][8] = { 0, };
	int cnt = 0; 
	queue <pair<int, int>>birus; 
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (map[i][j] == 2)birus.push({ i,j }); 
			if (wall[i][j] == 1)s_map[i][j] = 1;
			else s_map[i][j] = map[i][j]; 
		}
	}
	while (!birus.empty()) {
		int by, bx; 
		tie(by, bx) = birus.front(); 
		//cout << by << " " << bx << endl; 
		birus.pop(); 
		q.push({ by,bx });
		visited[by][bx] = 1; 
		while (!q.empty()) {
			int y, x; 
			tie(y, x) = q.front(); 			
			q.pop(); 
			for (int i = 0; i < 4; i++) {
				int ny = dy[i] + y; 
				int nx = dx[i] + x; 
				if (nx < 0 || nx >= n || ny < 0 || ny >= m)continue; 
				if (s_map[ny][nx] == 1 ||s_map[ny][nx]==2|| visited[ny][nx])continue; 
				q.push({ ny,nx }); 
				visited[ny][nx] = 1; 
				cnt++;
			}
		}
	}
	max_cnt = max(path_cnt-cnt-3, max_cnt); 
	return; 
}
void w_check(int cnt) {
	if (cnt == 3) {
		bfs(); 
		return; 	
	}
	else {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (map[i][j] == 0&&wall[i][j]==0) {
					wall[i][j] = 1; //벽을 의미	
					w_check(cnt + 1); 
					wall[i][j] = 0; 
				}
			}
		}
	}
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> m >> n; 
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			cin >> map[i][j]; 		
			if (map[i][j] == 2 || map[i][j] == 1)wall[i][j] = -1;
			else path_cnt++; 
			//2 바이러스, 1 벽, 0 통로 
		}
	}
	w_check(0); 
	cout << max_cnt; 
}
 

14502번: 연구소

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

www.acmicpc.net

 

반응형

'백준 > 완전 탐색' 카테고리의 다른 글

[백준 2529번 / C++] 부등호  (0) 2022.02.19
[백준 1107번 / C++] 리모컨  (0) 2022.02.14
[백준 14620번 / C++] 꽃길  (0) 2022.01.25
[백준 1189번 / C++] 컴백홈  (0) 2022.01.25
[백준 2589번 / C++] 보물섬  (0) 2022.01.14

댓글