2 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅ‡ Gold 5


๐Ÿ“Œ ๋ฌธ์ œ

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

image

image

image


๐Ÿ“Œ ํ’€์ด

์™€ ์ง€๊ธˆ๊นŒ์ง€์˜ BFS๋ž‘ ๊ฒฐ์ด ๋‹ค๋ฅธ ๋ฌธ์ œ์˜€๋‹ค.
ํ™•์‹คํžˆ ๋‚œ์ด๋„๊ฐ€ ๊ณจ๋“œ๊ธ‰ ๋˜๋Š” ๋ฌธ์ œ๋ฅผ ํ‘ธ๋‹ˆ๊นŒ, ์ง€๊ธˆ๊นŒ์ง€์˜ BFS์˜ ์ ‘๊ทผ ๋ฐฉ๋ฒ•๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋‚˜์™”๋‹ค.
์ง€๊ธˆ๊นŒ์ง€๋Š” ๋‹จ์ˆœํžˆ start ์ง€์ ์—์„œ end ์ง€์ ๊นŒ์ง€์˜ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๊ฐ™์€ ํƒ€์ž…์˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” start๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ด๊ณ , end๊ฐ€ ๋”ฐ๋กœ ์—†์ด ์ข…๋ฃŒ ์‹œ์ ์„ ์ •ํ•ด์ค˜์•ผํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

๊ทธ๋ž˜์„œ ์ฒ˜์Œ ๊ตฌํ˜„์„ ํ• ๋•Œ๋Š” ์ด์ „๊ณผ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„์„ ํ–ˆ๋‹ค๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ฒผ๋‹ค.
๋ฐ”๋กœ 1์ด ์—ฌ๋Ÿฌ๊ฐœ ์žˆ์œผ๋ฉด ๋™์‹œ์— ํผ์ ธ๋‚˜๊ฐ€์•ผํ•˜๋Š”๋ฐ, ์•ž์˜ ์ฃผ์†Œ๊ฐ’์— ์žˆ๋Š” 1์—์„œ ๋‹ค ํผ์ ธ๋‚˜๊ฐ„ํ›„ ๋’ค์—๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ์‹์œผ๋กœ ์ž‘๋™์„ ํ–ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋Œ€์ฒด ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•ด์•ผํ• ๊นŒ ๊ณ ๋ฏผ์„ ํ•˜๋‹ค๊ฐ€, ์ฒ˜์Œ์—๋Š” ์ด BFS์— ๋”ฐ๋กœ curtime์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์–ด์„œ, box[i][j] == curtime์ผ ๋•Œ๋งŒ ํผ์ ธ๋‚˜๊ฐ€๋Š” ์‹์œผ๋กœ ๊ตฌํ˜„์„ ํ•˜์˜€๋‹ค.
ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฐ์‹์œผ๋กœ ํ•˜๋‹ˆ๊นŒ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ o(n^3)์ด๋ผ๋Š” ๋ง๋„ ์•ˆ๋˜๊ฒŒ ๋Š๋ฆฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋˜์–ด๋ฒ„๋ ธ๋‹ค.

๊ฒฐ๊ตญ ๊ณ ์‹ฌํ•œ ๋์—, ๋นˆ๊ณต๊ฐ„์„ ํ† ๋งˆํ† ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ์ตœ๋Œ€์‹œ๊ฐ„์ธ 1000000 ๋ณด๋‹ค ๋งŽ๊ฒŒ empty๋ผ๋Š” ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฐ”๊พธ์–ด์ฃผ๊ณ , Math.min์œผ๋กœ ๋นˆ๊ณต๊ฐ„๊ณผ ์ฑ„์šฐ๋Š”๋ฐ ๊ฑธ๋ฆฐ์‹œ๊ฐ„์„ ๋น„๊ตํ•ด์„œ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
์ด๋Ÿฐ์‹์œผ๋กœํ•˜๋ฉด ์ด๋ฏธ ๊ฐ’์ด ๋“ค์–ด์žˆ์–ด๋„ ๋” ์ž‘์€ ์‹œ๊ฐ„์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ, ์ฝ”๋“œ๊ฐ€ ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ์ •์ƒ์ ์œผ๋กœ ํผ์ง„ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋œ๋‹ค.

์ด์ œ ์Šฌ์Šฌ ์ƒ์œ„ ๊ณจ๋“œ๋ฌธ์ œ๋ฅผ ํ’€ ์‹œ๊ธฐ๊ฐ€ ์˜จ ๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿ˜Ž


๐Ÿ“Œ Code

package BOJ.BFS;

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

public class N7576 {
    static int[][] box;
    static Queue<Integer[]> q;
    static int M;
    static int N;
    static int[] DR = new int[]{-1, 0, 1, 0};
    static int[] DC = new int[]{0, 1, 0, -1};
    static final int empty = 1000002;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());
        box = new int[N][M];
        q = new LinkedList<>();

        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                box[i][j] = Integer.parseInt(st.nextToken());
                if (box[i][j] == 1) {
                    q.add(new Integer[]{i, j});
                } else if (box[i][j] == 0) {
                    box[i][j] = empty;
                }

            }
        }
        BFS();
        FindTomato();
    }

    static void FindTomato() {
        int max = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                max = Math.max(max, box[i][j]);
            }
        }
        if (max == empty) {
            System.out.println(-1);
        } else {
            System.out.println(max - 1);
        }
    }

    static void BFS() {
        Integer[] cur;
        int nextRow;
        int nextCol;
        int curtime;
        while (!q.isEmpty()) {
            cur = q.poll();
            curtime = box[cur[0]][cur[1]] + 1;
            for (int i = 0; i < DC.length; i++) {
                nextRow = cur[0] + DR[i];
                nextCol = cur[1] + DC[i];
                if (nextRow < 0 || nextRow >= N || nextCol < 0 || nextCol >= M || box[nextRow][nextCol] != empty) {
                    continue;
                }
                q.add(new Integer[]{nextRow, nextCol});
                box[nextRow][nextCol] = Math.min(box[nextRow][nextCol], curtime);
            }
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜