골드 5
https://www.acmicpc.net/problem/14502
package 그래프탐색;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ14502 {
public static int n, m;
public static int[] dx = {-1, 1, 0, 0};
public static int[] dy = {0, 0, -1, 1};
public static int[][] map;
public static int[][] virusMap;
public static int max = Integer.MIN_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0);
System.out.println(max);
}
// 벽세우기=완탐=백트래킹
public static void dfs(int wall) {
if (wall == 3) {
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;
dfs(wall + 1);
map[i][j] = 0;
}
}
}
}
// 바이러스 퍼뜨리기
public static void bfs() {
Queue<int[]> q = new LinkedList<>();
virusMap = new int[n][m];
// virusMap 만드는 이유
// 벽이 세개 세워질 때마다 bfs를 실행해야하므로
// 벽이 세워진 map을 복사해서 써야함 (초기화 용도)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
virusMap[i][j] = map[i][j];
if (virusMap[i][j] == 2) {
q.add(new int[]{i, j});
}
}
}
// // map 복제
//int[][] virusMap = new int[n][m];
//for (int i = 0; i < n; i++) {
// virusMap[i] = Arrays.copyOf(map[i], m);
//}
while (!q.isEmpty()) {
int[] tmp = q.poll();
int x = tmp[0];
int y = tmp[1];
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 && virusMap[nx][ny] == 0) {
virusMap[nx][ny] = 2;
q.add(new int[]{nx, ny});
}
}
}
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (virusMap[i][j] == 0) {
count++;
}
}
}
max = Math.max(max, count);
}
}