https://www.acmicpc.net/problem/14502
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int map[8][8];
int map_cp[8][8];
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };
int N, M;
int ans=0;
vector <pair<int, int>> virus;
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 i = 0; i < 4; i++){
int cx = x + dx[i];
int cy = y + dy[i];
if (cx >= 0 && cx <= N-1 && cy >= 0 && cy <= M-1) {
if (!map_cp[cx][cy]) {
map_cp[cx][cy] = 2;
q.push(make_pair(cx, cy));
}
}
}
}
int tmp = 0;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++) {
if (map_cp[i][j] == 0) {
tmp++;
}
}
}
ans = max(ans, tmp);
}
void makewall(int cnt) {
if (cnt == 3) {
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++) {
map_cp[i][j] = map[i][j];
}
}
bfs();
return;
}
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++) {
if (map[i][j] == 0) {
map[i][j] = 1;
makewall(cnt + 1);
map[i][j] = 0;
}
}
}
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++) {
cin >> map[i][j];
if (map[i][j] == 2) virus.push_back(make_pair(i, j));
}
}
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++) {
if (map[i][j] == 0) {
map[i][j] = 1;
makewall(1);
map[i][j] = 0;
}
}
}
cout << ans;
return 0;
}
'Algorithm > 구현' 카테고리의 다른 글
[프로그래머스] 코딩테스트 고득점 Kit - 정렬 (JAVA) (0) | 2021.10.01 |
---|---|
[백준] 2671번 : 잠수함식별 (C++) (0) | 2021.09.25 |
[백준] 2003번 : 수들의 합 2 (C++) (0) | 2021.09.22 |
[백준] 11005번 : 진법 변환 2 (C++, JAVA) (0) | 2021.09.21 |
[백준] 17837번 : 새로운 게임 2 (C++) (삼성 SW 역량테스트) (0) | 2021.09.19 |
댓글