formula
딱히 없음
벽 3개 세우는 거를 완전탐색 돌려야하니까 백트래킹 필요한거랑
벽 세워진거 복사해서 전파해서 최댓값 구하는 문제
Implementation
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
int N, M;
int board[8][8];
int temp[8][8]; // Case 별용
int ans = 0; // res
// 상 하 좌 우
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };
void bfs() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
temp[i][j] = board[i][j];
}
}
// 바이러스 전파 bfs
queue<pair<int, int>> q;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (temp[i][j] == 2) {
q.push({ i, j });
}
}
}
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < N && ny >= 0 && ny < M) {
if (temp[nx][ny] == 0) {
temp[nx][ny] = 2;
q.push({ nx, ny });
}
}
}
}
// 안전 영역 도출
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (temp[i][j] == 0) cnt++;
}
}
ans = max(ans, cnt);
}
// 벽 브루트포스
void makeWall(int cnt) {
if (cnt == 3) {
bfs();
return;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (board[i][j] == 0) {
board[i][j] = 1; // 벽
makeWall(cnt + 1);
board[i][j] = 0; // Backtraking
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> board[i][j];
}
}
// 브루트포스
makeWall(0);
cout << ans << endl;
return 0;
}