|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class DH_방화벽_설치하기 { |
| 5 | + static class Point { |
| 6 | + int r, c; |
| 7 | + public Point(int r, int c) { |
| 8 | + this.r = r; |
| 9 | + this.c = c; |
| 10 | + } |
| 11 | + } |
| 12 | + static int N, M, emptyArea, result; |
| 13 | + static int[][] map; |
| 14 | + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; |
| 15 | + |
| 16 | + static void solution() { |
| 17 | + // 방화벽 3개 추가 설치 (조합) |
| 18 | + func(0, 0, new boolean[N][M]); |
| 19 | + System.out.println(result); |
| 20 | + } |
| 21 | + |
| 22 | + static int bfs(boolean[][] wall) { |
| 23 | + Deque<Point> q = new ArrayDeque<Point>(); |
| 24 | + |
| 25 | + int cnt = 0; // 불에 타는 영역 수 |
| 26 | + boolean[][] v = new boolean[N][M]; |
| 27 | + |
| 28 | + for(int r = 0; r < N; r++) { |
| 29 | + for(int c = 0; c < M; c++) { |
| 30 | + // 이미 방문한 곳이거나, 불이 아니거나, 벽일 때 |
| 31 | + if(v[r][c] || map[r][c] != 2 || wall[r][c]) continue; |
| 32 | + q.add(new Point(r, c)); |
| 33 | + v[r][c] = true; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + while(!q.isEmpty()) { |
| 38 | + Point current = q.poll(); |
| 39 | + |
| 40 | + for(int d = 0; d < 4; d++) { |
| 41 | + int nr = current.r + dr[d]; |
| 42 | + int nc = current.c + dc[d]; |
| 43 | + |
| 44 | + // 범위에 벗어나거나, 이미 간 곳이거나, 벽이면 못감 |
| 45 | + if(!check(nr, nc) || v[nr][nc] || map[nr][nc] == 1 || wall[nr][nc]) continue; |
| 46 | + q.add(new Point(nr, nc)); |
| 47 | + v[nr][nc]= true; |
| 48 | + cnt++; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return cnt; |
| 53 | + } |
| 54 | + |
| 55 | + static boolean check(int r, int c) { |
| 56 | + return r >= 0 && r < N && c >= 0 && c < M; |
| 57 | + } |
| 58 | + static void func(int idx, int depth, boolean[][] wall) { |
| 59 | + if(depth == 3) { |
| 60 | + |
| 61 | + int fireArea = bfs(wall); |
| 62 | + result = Math.max(result, emptyArea - 3 - fireArea); |
| 63 | + |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + for(int i = idx; i < N * M; i++) { |
| 68 | + int r = i / M; |
| 69 | + int c = i % M; |
| 70 | + |
| 71 | + // 이미 방화벽이 있거나, 불이 있는 곳에는 방화벽을 세울 수 없음 |
| 72 | + if(map[r][c] != 0) continue; |
| 73 | + |
| 74 | + wall[r][c] = true; |
| 75 | + func(i + 1, depth + 1, wall); |
| 76 | + wall[r][c] = false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static void main(String[] args) throws Exception { |
| 81 | + System.setIn(new FileInputStream("./input/방화벽설치하기.txt")); |
| 82 | + initInput(); |
| 83 | + solution(); |
| 84 | + } |
| 85 | + |
| 86 | + static void initInput() throws Exception { |
| 87 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 88 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 89 | + |
| 90 | + result = Integer.MIN_VALUE; |
| 91 | + |
| 92 | + N = Integer.parseInt(st.nextToken()); |
| 93 | + M = Integer.parseInt(st.nextToken()); |
| 94 | + |
| 95 | + map = new int[N][M]; |
| 96 | + |
| 97 | + for(int r = 0; r < N; r++) { |
| 98 | + st = new StringTokenizer(br.readLine()); |
| 99 | + for(int c = 0; c < M; c++) { |
| 100 | + map[r][c] = Integer.parseInt(st.nextToken()); |
| 101 | + if(map[r][c] == 0) emptyArea++; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments