|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + |
| 6 | + private static int[][] virus; |
| 7 | + private static int N, M; |
| 8 | + private static List<int[]> space; |
| 9 | + private static int answer = 0; |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + N = Integer.parseInt(st.nextToken()); |
| 15 | + M = Integer.parseInt(st.nextToken()); |
| 16 | + virus = new int[N][M]; |
| 17 | + space = new ArrayList<>(); |
| 18 | + for(int i=0; i<N; i++) { |
| 19 | + st = new StringTokenizer(br.readLine()); |
| 20 | + for(int j=0; j<M; j++) { |
| 21 | + virus[i][j] = Integer.parseInt(st.nextToken()); |
| 22 | + if(virus[i][j] == 0) { |
| 23 | + space.add(new int[]{i, j}); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + dfs(0, 0, new ArrayList<>()); |
| 28 | + System.out.println(answer); |
| 29 | + } |
| 30 | + |
| 31 | + public static void dfs(int start, int index, List<int[]> wall) { |
| 32 | + if(index == 3) { |
| 33 | + answer = Math.max(answer, bfs(copyArr(), wall)); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + for(int i=start; i<space.size(); i++) { |
| 38 | + int h = space.get(i)[0]; |
| 39 | + int w = space.get(i)[1]; |
| 40 | + wall.add(new int[]{h, w}); |
| 41 | + dfs(i+1, index+1, wall); |
| 42 | + wall.remove(index); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static int bfs(int[][] virus, List<int[]> wall) { |
| 47 | + int[] dh = {0, 1, 0, -1}; |
| 48 | + int[] dw = {-1, 0, 1, 0}; |
| 49 | + Queue<int[]> q = new LinkedList<>(); |
| 50 | + boolean[][] visited = new boolean[N][M]; |
| 51 | + for(int i=0; i<3; i++) { |
| 52 | + virus[wall.get(i)[0]][wall.get(i)[1]] = 1; |
| 53 | + } |
| 54 | + for(int i=0; i<N; i++) { |
| 55 | + for(int j=0; j<M; j++) { |
| 56 | + if(virus[i][j] == 2) { |
| 57 | + q.offer(new int[]{i, j}); |
| 58 | + visited[i][j] = true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + while(!q.isEmpty()) { |
| 63 | + int h = q.peek()[0]; |
| 64 | + int w = q.peek()[1]; |
| 65 | + q.poll(); |
| 66 | + for(int i=0; i<4; i++) { |
| 67 | + int nextH = h + dh[i]; |
| 68 | + int nextW = w + dw[i]; |
| 69 | + if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < M) { |
| 70 | + if(!visited[nextH][nextW] && virus[nextH][nextW] == 0) { |
| 71 | + visited[nextH][nextW] = true; |
| 72 | + virus[nextH][nextW] = 2; |
| 73 | + q.offer(new int[]{nextH, nextW}); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return getSafeZone(virus); |
| 79 | + } |
| 80 | + |
| 81 | + public static int[][] copyArr() { |
| 82 | + int[][] arr = new int[N][M]; |
| 83 | + for(int i=0; i<N; i++) { |
| 84 | + arr[i] = virus[i].clone(); |
| 85 | + } |
| 86 | + return arr; |
| 87 | + } |
| 88 | + |
| 89 | + public static int getSafeZone(int[][] virus) { |
| 90 | + int count = 0; |
| 91 | + for(int i=0; i<N; i++) { |
| 92 | + for(int j=0; j<M; j++) { |
| 93 | + if(virus[i][j] == 0) { |
| 94 | + count++; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return count; |
| 99 | + } |
| 100 | +} |
0 commit comments