๊ธฐ๋ก๋ฐฉ

BOJ_4179 : ๋ถˆ! ๋ณธ๋ฌธ

CodingTest/Java

BOJ_4179 : ๋ถˆ!

Soom_1n 2024. 6. 11. 18:54

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



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

  • R x C ๊ฒฉ์žํŒ์—์„œ ์ง€ํ›ˆ์ด์˜ ์œ„์น˜์™€ ๋ถˆ๋“ค์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ์ง€ํ›ˆ์ด๋Š” 1๋ถ„์— ์ƒํ•˜์ขŒ์šฐ 4๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ถˆ๋„ 1๋ถ„์— 4๋ฐฉํ–ฅ์œผ๋กœ ํ™•์‚ฐ๋œ๋‹ค.
  • ๊ฒฉ์žํŒ์˜ ๊ฐ€์žฅ์ž๋ฆฌ๋กœ ๊ฐ€๋ฉด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ง€ํ›ˆ์ด๊ฐ€ ๋ถˆ์„ ํ”ผํ•ด ๋ฏธ๋กœ๋ฅผ ํƒˆ์ถœํ•˜๊ธฐ ์œ„ํ•œ ์ตœ๋‹จ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.
    ๋งŒ์•ฝ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด "IMPOSSIBLE"์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • ๊ฒฉ์žํŒ์—์„œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ๋‹จ์ˆœ BFS ๋ฌธ์ œ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ง€ํ›ˆ์ด์˜ ์ด๋™๊ณผ ๋ถˆ์˜ ํ™•์‚ฐ ๋ชจ๋‘ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.
    • ํ๋ฅผ ์ด์šฉํ•œ BFS ๊ตฌํ˜„์—์„œ ํ ์‚ฌ์ด์ฆˆ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ '1๋ถ„'์˜ ์›€์ง์ž„์„ ์ œํ•œํ•ด์•ผ ํ•œ๋‹ค.
  • ์ง€ํ›ˆ์ด์˜ ์œ„์น˜๋Š” ๋งต์— ๊ทธ๋ฆฌ์ง€ ์•Š๊ณ  ํ์—๋งŒ ๋„ฃ๊ณ , ๋ถˆ์€ ๋งต์— 'F'๋กœ ํ‘œ์‹œํ•ด ๊ฐ„๋‹ค.
    • ์ง€ํ›ˆ์ด๋Š” ๋ถˆ๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ํ์—์„œ ๊บผ๋ƒˆ์„ ๋•Œ ํ˜„์žฌ ์œ„์น˜๊ฐ€ 'F'๋กœ ๋˜์–ด์žˆ์œผ๋ฉด ๋ถˆ์— ์ง‘์–ด์‚ผ์ผœ์ง„ ๊ฒƒ์ด๋ฏ€๋กœ ๋‹ค์Œ ์›์†Œ๋ฅผ ํ™•์ธํ•œ๋‹ค.
  • ์ง€ํ›ˆ์ด๊ฐ€ ๊ฐ€์žฅ์ž๋ฆฌ๋กœ ์ด๋™ํ–ˆ๋‹ค๋ฉด ๋ฐ”๋กœ ์ •๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๋งŒ์•ฝ ํ๊ฐ€ ๋น„์–ด์งˆ ๋•Œ ๊นŒ์ง€ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•ด์„œ "IMPOSSIBLE"์„ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

public class Main {
    private static final int[] dx = {-1, 0, 1, 0};
    private static final int[] dy = {0, 1, 0, -1};
    private static int R, C;
    private static char[][] map;
    private static Queue<Point> run, burn;

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

        R = Integer.parseInt(st.nextToken());
        C = Integer.parseInt(st.nextToken());
        map = new char[R][C];

        run = new ArrayDeque<>();
        burn = new ArrayDeque<>();

        for (int i = 0; i < R; i++) {
            char[] chars = br.readLine().toCharArray();
            for (int j = 0; j < C; j++) {
                if (chars[j] == 'J') {
                    run.add(new Point(i, j));
                    map[i][j] = '.';
                } else if (chars[j] == 'F') {
                    burn.add(new Point(i, j));
                    map[i][j] = 'F';
                } else
                    map[i][j] = chars[j];
            }
        }

        // BFS
        int result = escape();

        // Output
        if (result == 0)
            bw.write("IMPOSSIBLE");
        else
            bw.write(Integer.toString(result));
        bw.flush();
    }

    private static int escape() {
        boolean[][] visited = new boolean[R][C];
        visited[run.peek().row][run.peek().col] = true;

        int cnt = 0;

        while (!run.isEmpty()) {
            int size = run.size();

            // ์ง€ํ›ˆ ์ด๋™
            for (int i = 0; i < size; i++) {
                Point jihun = run.poll();

                if (map[jihun.row][jihun.col] == 'F') {
                    continue;
                }

                for (int j = 0; j < 4; j++) {
                    int row = jihun.row + dx[j];
                    int col = jihun.col + dy[j];
                    if (0 > row || row >= R || 0 > col || col >= C) {
                        return cnt + 1;
                    }
                    if (!visited[row][col] && map[row][col] == '.') {
                        visited[row][col] = true;
                        run.add(new Point(row, col));
                    }
                }
            }
            cnt++;

            // ๋ถˆ ๋ฒˆ์ง
            size = burn.size();
            for (int i = 0; i < size; i++) {
                Point fire = burn.poll();
                for (int j = 0; j < 4; j++) {
                    int row = fire.row + dx[j];
                    int col = fire.col + dy[j];
                    if (0 <= row && row < R && 0 <= col && col < C && map[row][col] == '.') {
                        map[row][col] = 'F';
                        burn.add(new Point(row, col));
                    }
                }
            }
        }
        return 0;
    }

    private static class Point {
        int row, col;

        public Point(int row, int col) {
            this.row = row;
            this.col = col;
        }
    }
}

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

  • charํ˜• 2์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งต ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ์ง€ํ›ˆ์ด์˜ ์‹œ์ž‘ ์œ„์น˜๋Š” ์ขŒํ‘œ๋งŒ ํ์— ๋‹ด๊ณ  '.'์œผ๋กœ ๊ธฐ๋กํ•œ๋‹ค.
    • ๋ถˆ์˜ ์‹œ์ž‘ ์œ„์น˜๋„ ํ์— ๋‹ด์ง€๋งŒ, ๋งต์—๋Š” 'F'๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.
  • BFSํƒ์ƒ‰์€ escape() ๋ฉ”์„œ๋“œ์—์„œ ์ง„ํ–‰ํ•œ๋‹ค.
    • 1๋ถ„์˜ ์‹œ๊ฐ„ ํ๋ฆ„์„ cnt์— ๋ˆ„์ ํ•ด์„œ ๊ธฐ๋กํ•˜๊ณ , ์ง€ํ›ˆ์ด๊ฐ€ ํƒˆ์ถœํ–ˆ์„ ์‹œ cnt๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    • ์ง€ํ›ˆ์ด ์œ„์น˜ ํ์˜ ๋ชจ๋“  ์›์†Œ๋ฅผ ํ™•์ธํ–ˆ์„ ๋•Œ์—๋„ ํƒˆ์ถœํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฐ˜ํ™˜๊ฐ’์— ๋”ฐ๋ผ ์ตœ์†Œ ์‹œ๊ฐ„ ํ˜น์€ "IMPOSSIBLE"์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž์ฒด๋Š” BFS๋กœ ๊ฐ„๋‹จํ–ˆ์ง€๋งŒ, ์‹œ์ž‘ ํ•  ๋•Œ ๋ถˆ์˜ ์œ„์น˜๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๋†“์น˜๊ณ  ํ‹€๋ ธ๋‹ค.
    • ๋”์šฑ ๋ฌธ์ œ๋ฅผ ์ž์„ธํžˆ ์ฝ์„ ํ•„์š”๊ฐ€ ์žˆ๋‹ค... ๋‹ค์Œ ๋ฌธ์ œ์—์„œ๋Š” ์ œ์ถœ ์ „์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋„ ๋งŽ์ด ๋งŒ๋“ค๊ณ , ํ•œ ๋ฒˆ์— ๋งž์ถ”๋Š”๊ฑธ ๋ชฉํ‘œ๋กœ ํ•ด์•ผ๊ฒ ๋‹ค.
728x90