본문 바로가기
백준

[JAVA] 백준 2636 - 치즈

by 맴썰 2025. 9. 6.

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

2638 치즈와 정확히 똑같은 문제인데, 치즈가 다 녹기 전 마지막 개수를 세는 부분이 추가된 문제이다.

나이스

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static int[] dx = {1, -1, 0, 0};
    static int[] dy = {0, 0, 1, -1};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int[] temp = Arrays.stream(s.split(" ")).mapToInt(Integer::parseInt).toArray();
        int[][] map = new int[temp[0]][temp[1]];
        for (int i = 0; i < map.length; i++) {
            map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        }
        int count = 0;
        int num = 0;
        while (!zeroCheck(map)) {
            boolean[][] visited = new boolean[map.length][map[0].length];
            //1. 빈공간 판단
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[0].length; j++) {
                    if (map[i][j] == 0 && !visited[i][j]) {
                        boolean check = dfs(map, i, j, visited, false);
                    }
                }
            }
            //2. 녹는 치즈 판단
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[0].length; j++) {
                    if (map[i][j] == 1) {
                        if (meltCheck(map, i, j)) {
                            map[i][j] = 3;
                        }
                    }
                }
            }
            count++;
            num = Math.max(meltOnly(map), num);
            convert(map);
        }
        System.out.println(count);
        System.out.println(num);
    }

    static boolean dfs(int[][] map, int i, int j, boolean[][] visited, boolean bef) {
        boolean isBoundary = i == 0 || j == 0 || bef;
        for (int k = 0; k < 4; k++) {
            boolean isZero = check(map, i + dx[k], j + dy[k], visited);
            if (isZero) {
                visited[i + dx[k]][j + dy[k]] = true;
                isBoundary = isBoundary | dfs(map, i + dx[k], j + dy[k], visited, isBoundary);
            }
        }
        if (!isBoundary) map[i][j] = 2;
        return isBoundary;
    }

    static boolean check(int[][] map, int i, int j, boolean[][] visited) {
        if (!boundaryCheck(map, i, j)) return false;
        return !visited[i][j];
    }

    static boolean meltCheck(int[][] map, int i, int j) {
        int count = 0;
        for (int k = 0; k < 4; k++) {
            if (boundaryCheck(map, i + dx[k], j + dy[k])) count++;
        }
        return count >= 1;
    }

    static boolean boundaryCheck(int[][] map, int i, int j) {
        if (i < 0 || i >= map.length || j < 0 || j >= map[0].length) return false;
        return map[i][j] == 0;
    }

    static boolean zeroCheck(int[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    static void convert(int[][] map) {
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j] > 1) {
                    map[i][j] = 0;
                }
            }
        }
    }

    static int meltOnly(int[][] map) {
        boolean isExist3 = false;
        int count = 0;
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map[0].length; j++) {
                if (map[i][j] == 0) {
                    continue;
                }
                if (map[i][j] != 3&&map[i][j] != 2) {
                    return -1;
                } else {
                    isExist3 = true;
                    if (map[i][j]==3)count++;
                }
            }
        }
        return isExist3 ? count : -1;
    }
}