[BOJ] 2636번 치즈 - JAVA

최영환·2023년 4월 2일
0

BaekJoon

목록 보기
54/87
post-thumbnail

💡 문제



💬 입출력 예시

📌 풀이(소스코드)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * 0,0 에서 BFS 탐색 시작(방문하지 않고, 0인 지점에서 시작)
 * 0이면 큐에 넣고 계속 탐색, 방문처리
 * 1이면 해당 위치를 0으로 바꾸고 방문처리, 큐에 넣지 않음
 * 방문배열 초기화
 * 완료 체크
 */
public class Main {

    static class Pos {
        int r;
        int c;

        public Pos(int r, int c) {
            this.r = r;
            this.c = c;
        }
    }

    static int[] dr = {-1, 1, 0, 0};
    static int[] dc = {0, 0, -1, 1};
    static int N, M, time, cnt;
    static int[][] map;
    static boolean[][] used;

    public static void main(String[] args) throws IOException {
        init();
        process();
        print();

    }

    private static void init() 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());
        map = new int[N][M];
        time = 1;

        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());
            }
        }
    }

    private static void process() {
        while (true) {
            used = new boolean[N][M];
            bfs();
            if (checkIsDone()) break;
            cnt = 0;
            time++;
        }
    }

    private static void print() {
        System.out.println(time);
        System.out.println(cnt);
    }

    private static boolean checkIsDone() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (map[i][j] != 0) return false;
            }
        }
        return true;
    }

    private static void bfs() {
        Queue<Pos> q = new LinkedList<>();
        q.add(new Pos(0, 0));
        used[0][0] = true;
        while (!q.isEmpty()) {
            Pos now = q.poll();
            for (int i = 0; i < 4; i++) {
                int nr = now.r + dr[i];
                int nc = now.c + dc[i];
                if (isOutOfRange(nr, nc) || used[nr][nc]) continue;
                if (map[nr][nc] == 1) {
                    map[nr][nc] = 0;
                    used[nr][nc] = true;
                    cnt++;
                    continue;
                }
                used[nr][nc] = true;
                q.add(new Pos(nr, nc));
            }
        }
    }

    private static boolean isOutOfRange(int nr, int nc) {
        return nr < 0 || nc < 0 || nr >= N || nc >= M;
    }
}

📄 해설

  • BFS 를 사용하여 푸는 문제로, 플러드필(Flood Fill) 유형의 응용이라 볼 수 있음
  • 바깥 공기와 접촉을 한 치즈만 녹아야하므로, 치즈의 위치에서 BFS 를 수행하면 안되고, 바깥 공기인 0, 0 지점에서부터 BFS 탐색을 수행해야함
  • 0 은 공기, 1 은 치즈로 표현
  • 치즈가 다 녹기까지의 시간은 time, 다 녹기 전의 치즈의 개수는 cnt 에 담아 저장
  • 모든 치즈가 녹을 때까지의 수행을 위해 while 문으로 무한 루프를 돌리고, BFS 탐색이 끝날 때마다 모든 치즈가 녹았는지를 확인
    • 이때, 모든 치즈가 녹았으면 반복을 종료하고, 모든 치즈가 녹지 않았으면 cnt 값을 초기화하고, time 값을 증가시킴
  • BFS 탐색의 절차는 아래와 같음
    1. 해당 위치가 1 이라면 해당 위치를 0 으로 바꾸고, cnt 값 증가와 방문처리를 한다.
    2. 해당 위치가 0 이라면 해당 위치를 큐에 넣고 방문처리를 한다.

어려웠던 부분

  • 바깥 공기와 안쪽 공기를 어떻게 구분을 하는지에 대한 접근이 어려웠으나, 이 접근 이후에는 어렵다는 느낌은 받지 못함
profile
조금 느릴게요~

0개의 댓글