๊ธฐ๋ก๋ฐฉ

BOJ_2573 : ๋น™์‚ฐ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2573 : ๋น™์‚ฐ

Soom_1n 2024. 4. 27. 18:55

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋ฐ”๋‹ท๋ฌผ๊ณผ ๋งž๋‹ฟ์€ ๋น™ํ•˜๋Š” 1๋…„๋งˆ๋‹ค ์ƒํ•˜์ขŒ์šฐ์˜ ๋ฐ”๋‹ท๋ฌผ ์ˆ˜ ๋งŒํผ ์ค„์–ด๋“ ๋‹ค.
  • ๋น™ํ•˜๊ฐ€ ๋‘ ๋ฉ์–ด๋ฆฌ ์ด์ƒ์œผ๋กœ ๋‚˜๋‰˜์–ด์ง€๋Š” ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ๋๊นŒ์ง€ ๋‘ ๋ฉ์–ด๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ๋น™ํ•˜๊ฐ€ ๋…น๋Š”๊ฑด 4๋ฐฉํƒ์ƒ‰์„ ํ•˜๋ฉด ๋˜๊ณ , ๋ฉ์–ด๋ฆฌ ํ™•์ธ์€ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰๊ณผ 4๋ฐฉ ํƒ์ƒ‰์„ ํ•จ๊ผ ํ•˜๋ฉด ๋œ๋‹ค.
  • ์—ฌ๊ธฐ์„œ๋Š” ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS) ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.*;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

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

    public static void main(String[] args) throws IOException {
        // Input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        map = new int[N][M]; // ๋ฒ”์œ„ ๋ฒ—์–ด๋‚˜๋Š”๊ฑฐ ์‹ ๊ฒฝ ์•ˆ์“ฐ๋ ค๊ณ  +2๋กœ ํŒจ๋”ฉ ๊ฐ์‹ธ๊ธฐ

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

        int answer = 0;
        while (true) {
            nextYear(); // ๋น™์‚ฐ ๋…น์Œ
            answer++;

            int result = check(); // BFS ๋น™์‚ฐ ๋ถ„๋ฆฌ ํ™•์ธ
            if (result >= 2) break;
            else if (result == 0) {
                answer = 0;
                break;
            }
        }

        // Output
        bw.write(Integer.toString(answer));
        bw.flush();
    }

    private static int check() {
        boolean[][] flag = new boolean[N][M];
        Queue<Point> que = new ArrayDeque<>();

        int result = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (map[i][j] > 0 && !flag[i][j]) {
                    flag[i][j] = true;
                    que.add(new Point(i, j));
                    result++;

                    while (!que.isEmpty()) {
                        Point p = que.poll();
                        for (int k = 0; k < 4; k++) {
                            int x = p.x + dx[k];
                            int y = p.y + dy[k];
                            if (0 <= x && x < N && 0 <= y && y < M && !flag[x][y] && map[x][y] > 0) {
                                flag[x][y] = true;
                                que.add(new Point(x, y));
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    private static void nextYear() {
        boolean[][] flag = new boolean[N][M];

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (map[i][j] > 0) {
                    flag[i][j] = true;
                    for (int k = 0; k < 4; k++) {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        if (0 <= x && x < N && 0 <= y && y < M && !flag[x][y] && map[x][y] == 0) {
                            map[i][j]--;
                        }
                    }
                    map[i][j] = Math.max(map[i][j], 0);
                }
            }
        }
    }

    private static class Point {
        int x, y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • nextYear() ๋ฉ”์„œ๋“œ์—์„œ 4๋ฐฉํƒ์ƒ‰์œผ๋กœ map ์— ๋…น์€ ๋น™์‚ฐ์„ ๊ธฐ๋กํ•œ๋‹ค.
  • check() ๋ฉ”์„œ๋“œ์—์„œ BFS๋กœ ๋น™์‚ฐ์˜ ๋ฉ์–ด๋ฆฌ ์ˆ˜๋ฅผ ๊ตฌํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  4๋ฐฉ ํƒ์ƒ‰์„ ํ•˜๋ฉด ๋์ด๋ผ ๋”ฑํžˆ ์–ด๋ ค์šธ ๊ฒƒ ์—†์ด ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

728x90