-

[BOJ - JAVA] 7569 - 토마토(BFS) 본문

백준 문제 풀이

[BOJ - JAVA] 7569 - 토마토(BFS)

흣차 2021. 11. 25. 23:22
728x90
반응형

# 주소

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

 

7569번: 토마토

첫 줄에는 상자의 크기를 나타내는 두 정수 M,N과 쌓아올려지는 상자의 수를 나타내는 H가 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M ≤ 100, 2 ≤ N ≤ 100,

www.acmicpc.net

# 문제

# 문제 해설 및 코드 리뷰

import java.util.*;
class Point{
	int x;
	int y;
	int z;
	Point(int x, int y, int z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
}
public class Main{
    public static int[] dx = {1,0,0,-1,0,0};
    public static int[] dy = {0,1,-1,0,0,0};
    public static int[] dz = {0,0,0,0,-1,1};
    public static int width, height, h;
    public static int count;
    public static boolean visit[][][];
    public static int arr[][][];
    public static Queue<Point> queue = new LinkedList<>();
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        height = scan.nextInt();
        width = scan.nextInt();
        h = scan.nextInt();
        count = 0;
        arr = new int[h][width][height];
        visit = new boolean[h][width][height];
        for(int k = 0; k < h; k++){
            for(int i = 0; i < width; i++){
                for(int j = 0; j < height; j++){
                    arr[k][i][j] = scan.nextInt();
                    if(arr[k][i][j] == 1){
                        queue.offer(new Point(k,i,j));
                        visit[k][i][j] = true;
                    }
                }
            }
        }
        while(!queue.isEmpty()){
            Point now = queue.poll();
            int x = now.x;
            int y = now.y;
            int z = now.z;
            for(int i = 0; i < 6; i++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                int nz = z + dz[i];
                if(0 <= nx && nx < h && 0 <= ny && ny < width && 0 <= nz && nz < height){
                    if(arr[nx][ny][nz] == 0 && visit[nx][ny][nz] == false){
                        queue.offer(new Point(nx,ny,nz));
                        arr[nx][ny][nz] = arr[x][y][z] + 1;
                    }
                }
            }
        }
        for(int k = 0; k < h; k++){
            for(int i = 0; i < width; i++){
                for(int j = 0; j < height; j++){
                    if(arr[k][i][j] == 0){
                        System.out.println(-1);
                        return;
                    }
                    if(count < arr[k][i][j]){
                        count = arr[k][i][j];
                    }
                }
            }
        }
        System.out.println(count-1);
    }
}

 

이전 문제랑 너무 똑같은 문제죠??? 이번엔 3차원 배열을 해야했기 때문에 생각할 것이 더 많았습니다.

아무래도 가야할 방향이 상하좌우만 있는 것이 아니라 앞과 뒤도 있기 때문입니다.

그 부분만 신경 써주신다면 

https://codingrapper.tistory.com/95

 

[BOJ - JAVA] 7576 - 토마토(BFS)

# 주소 https://www.acmicpc.net/problem/7576 7576번: 토마토 첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000..

codingrapper.tistory.com

이 문제랑 완전 똑같은 문제입니다.

너무 간단한거라 넘어갈게요. BFS도 슬슬 질리네요 ㅠㅠㅠ

728x90
반응형
Comments