인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다.
연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다.
일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다. 새로 세울 수 있는 벽의 개수는 3개이며, 꼭 3개를 세워야 한다.
예를 들어, 아래와 같이 연구소가 생긴 경우를 살펴보자.
2 0 0 0 1 1 0
0 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 0 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0
이때, 0은 빈 칸, 1은 벽, 2는 바이러스가 있는 곳이다. 아무런 벽을 세우지 않는다면, 바이러스는 모든 빈 칸으로 퍼져나갈 수 있다.
2행 1열, 1행 2열, 4행 6열에 벽을 세운다면 지도의 모양은 아래와 같아지게 된다.
2 1 0 0 1 1 0
1 0 1 0 1 2 0
0 1 1 0 1 0 0
0 1 0 0 0 1 0
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0
바이러스가 퍼진 뒤의 모습은 아래와 같아진다.
2 1 0 0 1 1 2
1 0 1 0 1 2 2
0 1 1 0 1 2 2
0 1 0 0 0 1 2
0 0 0 0 0 1 1
0 1 0 0 0 0 0
0 1 0 0 0 0 0
벽을 3개 세운 뒤, 바이러스가 퍼질 수 없는 곳을 안전 영역이라고 한다. 위의 지도에서 안전 영역의 크기는 27이다.
연구소의 지도가 주어졌을 때 얻을 수 있는 안전 영역 크기의 최댓값을 구하는 프로그램을 작성하시오.
너비 우선 탐색 브루트포스 알고리즘 백트래킹
package feb_week1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ14502 {
static int n;
static int m;
static int[][] virus;
static int[][] temp_virus;
static ArrayList<int[]> where_virus;
static boolean[][] visited;
static Queue<int[]> queue;
static int answer;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
virus = new int[n][m];
temp_virus = new int[n][m];
visited = new boolean[n][m];
queue = new LinkedList<int[]>();
where_virus = new ArrayList<int[]>();
// 초기
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
virus[i][j] = Integer.parseInt(st.nextToken());
if (virus[i][j] == 2) {
queue.offer(new int[] { i, j });
where_virus.add(new int[] {i, j});
}
}
}
back(0);
System.out.println(answer);
}
// 벽을 세운 뒤
public static void bfs() {
int[] dx = { -1, 1, 0, 0 };
int[] dy = { 0, 0, -1, 1 };
// 큐 초기화 후 바이러스 위치 다시 넣어주기
queue = new LinkedList<int[]>();
for (int i = 0; i < n; i++) {
temp_virus[i] = virus[i].clone();
}
for (int[] where : where_virus) {
queue.offer(where);
}
while (!queue.isEmpty()) {
int[] now = queue.poll();
for (int i = 0; i < 4; i++) {
int nx = now[0] + dx[i];
int ny = now[1] + dy[i];
if (0 <= nx && nx < n && 0 <= ny && ny < m) {
if (temp_virus[nx][ny] == 0) {
temp_virus[nx][ny] = 2; // 바이러스 퍼짐
queue.offer(new int[] {nx, ny});
}
}
}
}
}
public static void back(int depth) {
if (depth == 3) {
bfs();
int safe = 0;
// 안전 영역의 최대 크기 구하기
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (temp_virus[i][j] == 0) {
safe++;
}
}
}
if (answer < safe) answer = safe;
return;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (virus[i][j] == 0 && !visited[i][j]) {
virus[i][j] = 1;
visited[i][j] = true;
back(depth + 1);
visited[i][j] = false;
virus[i][j] = 0;
}
}
}
}
}