본문 바로가기
백준 알고리즘/BFS

연구소 C++

by paysmile 2020. 11. 16.

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>

using namespace std;

const int MAX = 8;
int n, m;
int lab[MAX][MAX];
int temp[MAX][MAX];
vector <pair<int, int>> virus;
vector <pair<int, int>> empty_room;

typedef struct {
	int x, y;
}Move;

Move mv[4] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

void copy_lab() {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			temp[i][j] = lab[i][j];
		}
	}
}

void bfs() {
	queue <pair<int, int>> q;
	for(int i=0; i<virus.size(); i++)
		q.push(make_pair(virus[i].first, virus[i].second));

	while (!q.empty()) {
		int x = q.front().first;
		int y = q.front().second;
		q.pop();

		for (int k = 0; k < 4; k++) {
			int mx = x + mv[k].x;
			int my = y + mv[k].y;

			if (mx >= 0 && mx < n && my >= 0 && my < m ) {
				if (temp[mx][my] == 0) {
					temp[mx][my] = 2;
					q.push(make_pair(mx, my));
				}
			}
		}
	}
}

int count_answer() {
	int count = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (temp[i][j] == 0)
				count++;
		}
	}
	return count;
}

int main(void) {
	int answer = 0;
	cin >> n >> m;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> lab[i][j];
			if (lab[i][j] == 2)
				virus.push_back(make_pair(i, j));
			else if (lab[i][j] == 0)
				empty_room.push_back(make_pair(i, j));
		}
	}

	for (int i = 0; i < empty_room.size() - 2; i++) {
		for (int j = i+1; j < empty_room.size() - 1; j++) {
			for (int k = j+1; k < empty_room.size(); k++) {
				copy_lab();
				temp[empty_room[i].first][empty_room[i].second] = 1;
				temp[empty_room[j].first][empty_room[j].second] = 1;
				temp[empty_room[k].first][empty_room[k].second] = 1;
				bfs();
				int temp_answer = count_answer();
				if (answer < temp_answer)
					answer = temp_answer;
			}
		}
	}
	cout << answer << endl;
}

 

'백준 알고리즘 > BFS' 카테고리의 다른 글

스타트 택시 C++  (0) 2021.03.02
아기상어 C++  (0) 2021.01.03
백준 구슬 탈출 2 C++  (0) 2019.10.18
백준 연구소3 C++  (0) 2019.10.18
백준 16234번 C++  (0) 2019.10.09