본문 바로가기
Algorithm Study/BaekJoon (JAVA)

백준 14502_연구소_JAVA

by 창브로 2024. 4. 21.
728x90

https://www.acmicpc.net/problem/14502

 

14502번: 연구소

인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크

www.acmicpc.net

import java.io.*;
import java.util.*;

public class Main {
    static int N, M;
    static int[][] grid;
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    static boolean[][] visited;
    static int answer = Integer.MIN_VALUE;

    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()); // 열

        grid = new int[N][M];
        visited = new boolean[N][M];

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                grid[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        // 벽을 깔고
        backtracking(0);

        System.out.print(answer);

    }

    private static void backtracking(int wallCount) {
        if (wallCount == 3) {
            bfs();
            return;
        }

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (grid[i][j] == 0) {
                    grid[i][j] = 1;
                    backtracking(wallCount + 1);
                    grid[i][j] = 0;
                }
            }
        }
    }

    private static void bfs() { // 바이러스 퍼트림
        Queue<int[]> queue = new LinkedList<>();

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (grid[i][j] == 2) {
                    queue.add(new int[]{i, j});
                }
            }
        }

        int copyGrid[][] = new int[N][M];

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                copyGrid[i][j] = grid[i][j];
            }
        }

        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int cur_x = cur[0];
            int cur_y = cur[1];

            for (int i = 0; i < 4; i++) {
                int next_x = cur_x + dx[i];
                int next_y = cur_y + dy[i];

                if (next_x >= 0 & next_y >= 0 && next_x < N && next_y < M && copyGrid[next_x][next_y] == 0) {
                    queue.add(new int[]{next_x, next_y});
                    copyGrid[next_x][next_y] = 2;
                }
            }
        }
        countSafeArea(copyGrid);
    }

    private static void countSafeArea(int[][] grid) {
        int count = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (grid[i][j] == 0) {
                    count++;
                }
            }
        }
        answer = Math.max(answer, count);
    }
}

 

회고

- 백트랙킹을 공부하고 나니 풀만했다.

'Algorithm Study > BaekJoon (JAVA)' 카테고리의 다른 글

백준 17608_막대기_JAVA  (0) 2024.04.23
백준 16236_아기 상어_JAVA  (0) 2024.04.22
백준 15686_치킨 배달_JAVA  (1) 2024.04.21
백준 8911_거북이_JAVA  (1) 2024.04.18
백준 2578_빙고_JAVA  (0) 2024.04.18