๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] ๋ฐฑ์ค€ 7576 - ํ† ๋งˆํ† 

by applemango2021 2020. 12. 12.

์ฒ˜์Œ ํ† ๋งˆํ†  ๋ฌธ์ œ ํ’€์—ˆ์„ ๋•Œ๋Š” '์ด๋ ‡๊ฒŒ ๊ผผ๊ผผํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋„ ์žˆ๋‹ค๋‹ˆ..!'๋ผ๋ฉด์„œ ๋†€๋ž๋˜ ๊ธฐ์–ต์ด ๋‚œ๋‹ค. 

 

์ด๋ฒˆ์— ๋ฌธ์ œ ํ’€ ๋•Œ์—๋Š” ํ† ๋งˆํ†  ์—ฌ๋ถ€์™€ ํ† ๋งˆํ†  ์ข…๋ฅ˜๋ฅผ ๋‹ด๋Š” tomato[][]์™€ ์ต๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ผ์ˆ˜๋ฅผ ๋‹ด์€ day[][] ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ๋งŒ๋“ค์—ˆ๋‹ค.

์˜ค๋ž˜ ๊ณ ๋ฏผํ•˜๊ธฐ ์‹ซ์–ด์„œ ๊ทธ๋žฌ๋Š”๋ฐ ์ด์ „์— ํ‘ผ ๋ฐฉ์‹์„ ๋ณด๋‹ˆ tomato[][]๋ฐฐ์—ด ํ•˜๋‚˜๋กœ ๋๋ƒˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ๋„ ํ›จ์”ฌ ์ ๊ฒŒ ๋“ค๊ณ , ์‹œ๊ฐ„๋„ ๋œ ๊ฑธ๋ฆฌ๋‹ˆ ๋ฐฐ์—ด ํ•˜๋‚˜๋กœ ํ‘ธ๋Š” ๊ฒŒ ์ข‹๊ธด ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

์ต์€ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋‹ค๋ฉด 1, ๊ทธ๋ƒฅ ํ† ๋งˆํ† ๊ฐ€ ์žˆ๋‹ค๋ฉด 0, ํ† ๋งˆํ† ๊ฐ€ ์—†๋‹ค๋ฉด -1์˜ ๊ฐ’์ด ๋“ค์–ด์˜จ๋‹ค๋Š” ์ ์—์„œ ์ฐฉ์•ˆํ•ด ์ฒ˜์Œ์— ์ต์€ ํ† ๋งˆํ† ๋“ค์„ BFS ํ์— ๋„ฃ์„ ๋•Œ ๋ฌด์กฐ๊ฑด dayCount๋ฅผ 2๋กœ ์„ธํŒ…ํ•ด์„œ ๋„ฃ์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋‹ค์Œ์— ๊ฐˆ ๊ณณ์˜ ํ† ๋งˆํ† ๊ฐ€ ์ด๋ฏธ ์ต์—ˆ๋Š”์ง€ ์•ˆ ์ต์—ˆ๋Š”์ง€ ํ•œ๋ฒˆ ๋” ์ฒดํฌํ•  ํ•„์š” ์—†์ด tomato[newN][newM]์ด 0์ด๋ฉด ๋ฌด์กฐ๊ฑด ํ์— ์ง‘์–ด๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

์ด์ „์— ์ง  ์ฝ”๋“œ๊ฐ€ ๋” ์ข‹๋„ค ใ…Ž

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

public class Main {

	public static int M;
	public static int N;
	public static int[][] tomato;
	public static Queue<int[]> queue = new LinkedList<int[]>();

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = null;
		str = br.readLine().trim().split(" ");
		M = Integer.parseInt(str[0]);
		N = Integer.parseInt(str[1]);
		
		tomato = new int[N][M];
		
		int totalTomato = 0;
		int rottenTomato = 0;
		for(int i=0;i<N;i++){
			str = br.readLine().trim().split(" ");
			for(int j=0;j<M;j++){
				tomato[i][j] = Integer.parseInt(str[j]);
				
				switch(tomato[i][j]){
				case 0:
					totalTomato++;
					break;
				case 1:
					totalTomato++;
					rottenTomato++;
					queue.offer(new int[]{i,j,2});
					break;
			
				}
			}
		}
		
		if(totalTomato==rottenTomato){
			System.out.println(0);
			return;
		}
		
		bfs();
		
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				if(tomato[i][j]==0){
					System.out.println(-1);
					return;
				}
			}
		}
		
		int max = Integer.MIN_VALUE;
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
				max = Math.max(max, tomato[i][j]);
			}
		}
		
		System.out.println(max-2);
	}//end main 
	
	public static int[][] direction = { {1,0}, {-1,0}, {0,1}, {0,-1} };
	private static void bfs() {
		int[] temp;
		int newX;
		int newY;
		int time;
		
		while(!queue.isEmpty()){
			temp = queue.poll();
			time = temp[2];
			
			for(int d=0;d<4;d++){
				newX = temp[0] + direction[d][0];
				newY = temp[1] + direction[d][1];
				
				if(newX>=0 && newX<N && newY>=0 && newY<M && tomato[newX][newY]==0){
					tomato[newX][newY] = time+1;
					queue.offer(new int[]{newX,newY,time+1});
				}
			}
		}
		
	}

}//end class