알고리즘

<백준> 7569번 자바 알고리즘 [BFS]

changha. 2022. 1. 17. 23:05
import java.io.*;
import java.util.*;


class PointXYZ {
    int height;
    int row;
    int colume;
    
    public PointXYZ(int height, int row, int colume){
        this.height = height;
        this.row = row;
        this.colume = colume;
    }
}


public class Main
{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static int rowArr[] = {-1, 0, 1, 0, 0, 0};
    static int colArr[] = {0, 1, 0, -1, 0, 0};
    static int heightArr[] = {0, 0, 0, 0, 1, -1};
    static int m, n, h;
    static int arr[][][];
    static Queue<PointXYZ> queue = new LinkedList<>(); 
	public static void main(String[] args) throws IOException{
		StringTokenizer st = new StringTokenizer(br.readLine());
		m = Integer.parseInt(st.nextToken());
        n = Integer.parseInt(st.nextToken());
        h = Integer.parseInt(st.nextToken());
        
        arr = new int[h + 1][n + 1][m + 1];
        
        for(int i = 1; i <= h; i++){
            for(int j = 1; j <= n; j++){
                st = new StringTokenizer(br.readLine());
                for(int k = 1; k <= m; k++){
                    arr[i][j][k] = Integer.parseInt(st.nextToken());
                    
                    if(arr[i][j][k] == 1) queue.add(new PointXYZ(i, j, k));
                }
            }
        }
        
        System.out.println(BFS());
	}
	
	public static int BFS(){
	    while(!queue.isEmpty()){
	        PointXYZ point = queue.poll();
	        
	        int height = point.height;
            int row = point.row;
            int col = point.colume;
            
            //6방향 
            for(int i = 0 ; i < 6; i++){
                int moveHeight = height + heightArr[i];
                int moveRow = row + rowArr[i];
                int moveCol = col + colArr[i];
                // 6방향으로 토마토가 익을 수 있는지 여부 확인
                if(checkPoint(moveHeight, moveRow, moveCol)){
                    // 익은 토마토를 큐에 추가
                    queue.add(new PointXYZ(moveHeight, moveRow, moveCol));
                    // 익은 토마토의 값 = 이전에 익은 토마토의 값 + 1
                    arr[moveHeight][moveRow][moveCol] = arr[height][row][col] + 1;
                }
            }
	    }
	    
	    int res = Integer.MIN_VALUE;
	    
	    for(int i = 1; i <= h; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= m; k++){
                    // 하나라도 익지 않은 토마토가 있다면 -1을 반환
                    if(arr[i][j][k] == 0) return -1;
                    // 토마토가 익는데 걸리는 시간을 구함
                    res = Math.max(res, arr[i][j][k]);
                }
            }
        }
        
        if(res == 1) return 0;
        else return (res - 1);
	}
	
	public static boolean checkPoint(int height, int row, int colume){
	    if(height < 1 || height > h || row < 1 || row > n || colume < 1 || colume > m) return false;
	    
	    if(arr[height][row][colume] == 0) return true;
	    else return false;
	}
}