πŸ“– [λ°±μ€€μ•Œκ³ λ¦¬μ¦˜ 풀이] Q.7576 ν† λ§ˆν†  λ¬Έμ œν’€μ΄

πŸ“– 문제

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

πŸ” 접근법

λͺ¨λ“  ν† λ§ˆν† κ°€ μ΅λŠ”λ° μ–Όλ§ˆλ‚˜ κ±Έλ¦¬λŠ”μ§€λ₯Ό κ΅¬ν•˜λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.

읡은 ν† λ§ˆν† μ™€ μΈμ ‘ν•΄μžˆλŠ” λœμ΅μ€ ν† λ§ˆν† λ“€μ€ 1일후에

μ΅μŠ΅λ‹ˆλ‹€. 즉 읡은 ν† λ§ˆν† μ™€ 거리가 1 둜 인접해 μžˆλŠ”

μ•ˆ 읡은 ν† λ§ˆν† λ“€μ„ bfs μ•Œκ³ λ¦¬μ¦˜ 탐색을 ν•˜λ©΄ 풀이가 κ°€λŠ₯ν•©λ‹ˆλ‹€.

ν˜„μž¬ 읡은 ν† λ§ˆν† λ₯Ό 큐에 λ„£μ–΄μ£Όκ³ 

μ°¨λ‘€λŒ€λ‘œ λ‹€λ₯Έ μΈμ ‘ν•œ μ•ˆ 읡은 ν† λ§ˆν† λ“€μ΄

읡을 λ•ŒκΉŒμ§€ bfs 탐색을 ν•΄μ€λ‹ˆλ‹€.

단, λ°°μΉ˜μ— 따라 λͺ¨λ“  ν† λ§ˆν† λ“€μ΄ 읡지 λͺ»ν•˜λŠ” κ²½μš°λ„ μžˆμ„ 수 μžˆμœΌλ‹ˆ

bfs 탐색후 λͺ¨λ“  ν† λ§ˆν† λ“€μ΄ μ΅μ—ˆλŠ”μ§€ 확인을 ν•΄μ€λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€.

πŸ’» μ½”λ“œ


package problem.bfs.Q7576;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

public class Main_7576 {
    static int n,m;
    static boolean [][] visited;
    static int [][] tomato;
    static Queue<int[]> queue = new LinkedList();
    static int moveX[] = {0,0,-1,1};
    static int moveY[] = {-1,1,0,0};

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        input();
        if(queue.isEmpty())
            bw.write(Integer.toString(-1));

        int day = bfs();
        if(checkAllTomato() == false)
            day = -1;

        bw.write(Integer.toString(day));

        bw.flush();
        bw.close();
    }
    public static void input() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String [] input = br.readLine().split(" ");
        m = Integer.parseInt(input[0]);
        n = Integer.parseInt(input[1]);
        visited = new boolean[n][m];
        tomato = new int[n][m];

        for(int i=0; i<n; i++){
            String temp[] = br.readLine().split(" ");
            for(int j=0; j<m; j++){
                tomato[i][j] = Integer.parseInt(temp[j]);
                if(tomato[i][j]==1)
                    queue.offer(new int[]{i,j});
            }
        }
    }

    public static int bfs () {
        int day = 1;
        while (!queue.isEmpty()){
            int coordinate[] = queue.poll();
            for(int i=0; i<4; i++){
                int x = coordinate[0] + moveX[i];
                int y = coordinate[1] + moveY[i];

                if ( x < 0 || y< 0 || x>=n || y>=m)
                    continue;
                if ( tomato[x][y] == -1 || visited[x][y] == true)
                    continue;

                if(tomato[x][y] == 0 && visited[x][y] == false) {
                    queue.offer(new int[]{x, y});
                    tomato[x][y] = tomato[coordinate[0]][coordinate[1]] + 1;
                    visited[x][y] = true;
                }
                day = Integer.max(tomato[x][y],day);
            }
        }
        return day-1;
    }
    public static boolean checkAllTomato() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(tomato[i][j] == 0)
                    return false;
            }
        }
        return true;
    }
}

Written on June 15, 2020