본문 바로가기
백준 알고리즘/브루트포스

백준 14502번 C++

by paysmile 2019. 4. 9.

#include 
#include 
#include 

using namespace std;

const int MAX = 8;
int n, m;
int lab[MAX][MAX];
int labcopy[MAX][MAX];
vector<pair<int, int>> emptyroom;

typedef struct {
int x, y;
}Move;
Move mv[4] = { {1,0},{-1,0},{0,1},{0,-1} };

int bfs() {
int answer = 0;
queue<pair<int, int>> q;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
labcopy[i][j] = lab[i][j];
if (lab[i][j] == 2)
q.push(make_pair(i, j));
}
}

while (!q.empty()) {
int currentx = q.front().first;
int currenty = q.front().second;
q.pop();

for (int k = 0; k < 4; k++) {
int movex = currentx + mv[k].x;
int movey = currenty + mv[k].y;

if (movex >= 0 && movex < n && movey >= 0 && movey < m) {
if (labcopy[movex][movey] == 0) {
labcopy[movex][movey] = 2;
q.push(make_pair(movex, movey));
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (labcopy[i][j] == 0)
answer++;
}
}
return answer;
}

int main(void) {
int maxsafe = 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] == 0)
emptyroom.push_back(make_pair(i, j));
}
}
for (int i = 0; i < emptyroom.size()-2; i++) {
for (int j = i + 1; j < emptyroom.size()-1; j++) {
for (int k = j + 1; k < emptyroom.size(); k++) {
pair<int, int> firstroom = emptyroom[i];
pair<int, int> secondroom = emptyroom[j];
pair<int, int> thirdroom = emptyroom[k];
lab[firstroom.first][firstroom.second] = 1;
lab[secondroom.first][secondroom.second] = 1;
lab[thirdroom.first][thirdroom.second] = 1;
maxsafe = max(maxsafe,bfs());
lab[firstroom.first][firstroom.second] = 0;
lab[secondroom.first][secondroom.second] = 0;
lab[thirdroom.first][thirdroom.second] = 0;
}
}
}
cout << maxsafe << endl;
return 0;
}

'백준 알고리즘 > 브루트포스' 카테고리의 다른 글

백준 2048 (Easy) C++  (0) 2019.10.19
백준 15686번 C++  (0) 2019.04.13
백준 1018번 C++  (0) 2019.02.04
백준 14889번 C++  (0) 2019.01.28
백준 14888번 C++  (0) 2019.01.28