[BOJ] 7569 -토마토(G5)

suhyun·2022년 12월 2일
0

백준/프로그래머스

목록 보기
43/81

문제 링크

7569-토마토


입력

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

둘째 줄부터는 가장 밑의 상자부터 가장 위의 상자까지에 저장된 토마토들의 정보가 주어진다.
즉, 둘째 줄부터 N개의 줄에는 하나의 상자에 담긴 토마토의 정보가 주어진다.
각 줄에는 상자 가로줄에 들어있는 토마토들의 상태가 M개의 정수로 주어진다.
정수 1은 익은 토마토, 정수 0 은 익지 않은 토마토, 정수 -1은 토마토가 들어있지 않은 칸을 나타낸다.
이러한 N개의 줄이 H번 반복하여 주어진다.

토마토가 하나 이상 있는 경우만 입력으로 주어진다.


출력

여러분은 토마토가 모두 익을 때까지 최소 며칠이 걸리는지를 계산해서 출력해야 한다.
만약, 저장될 때부터 모든 토마토가 익어있는 상태이면 0을 출력해야 하고, 토마토가 모두 익지는 못하는 상황이면 -1을 출력해야 한다.


문제 풀이

7576-토마토 와 아주 같은 문제
차이점이라면 2차원이냐 3차원이냐 정도
2차원을 먼저 풀어서 어렵지 않게 해결할 수 있었다.

3차원이라 인덱스값만 헷갈리지않게 조심!

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

class tomato {
    int x, y, z;

    tomato(int x, int y, int z) {
        this.z = z;
        this.y = y;
        this.x = x;
    }
}

public class Main {

    static int M, N, H;
    static int[] dx = { 0, 0, -1, 1, 0, 0 };
    static int[] dy = { 0, 0, 0, 0, 1, -1 };
    static int[] dz = { 1, -1, 0, 0, 0, 0 };
    static int[][][] tomatos;
    static Queue<tomato> queue;

    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());
        H = Integer.parseInt(st.nextToken());
        tomatos = new int[H][N][M];
        queue = new LinkedList<tomato>();

        for (int i = 0; i < H; i++) {
            for (int j = 0; j < N; j++) {
                st = new StringTokenizer(br.readLine());
                for (int k = 0; k < M; k++) {
                    tomatos[i][j][k] = Integer.parseInt(st.nextToken());
                    if (tomatos[i][j][k] == 1) {
                        queue.add(new tomato(k, j, i));
                    }
                }
            }
        }
        System.out.println(bfs());
    }

    static int bfs() {
        while (!queue.isEmpty()) {
            tomato tmp = queue.poll();

            int x = tmp.x;
            int y = tmp.y;
            int z = tmp.z;

            for (int i = 0; i < 6; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                int nz = z + dz[i];

                if (nx < 0 || ny < 0 || nz < 0 || nx >= M || ny >= N || nz >= H) {
                    continue;
                }
                if (tomatos[nz][ny][nx] == 0) {
                    tomatos[nz][ny][nx] = tomatos[z][y][x] + 1; // 익어진 날짜를 저장
                    queue.add(new tomato(nx, ny, nz));
                }
            }
        }
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < M; k++) {
                    if (tomatos[i][j][k] == 0) return -1;
                    max = Math.max(max, tomatos[i][j][k]);
                }
            }
        }
        return max - 1;
    }
}
profile
꾸준히 하려고 노력하는 편 💻

0개의 댓글