2 분 소요



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



🔍 문제 풀이

문제 도식화

arr 원본은 유지하고 v 배열로 방문 체크를 했는데, 벽도 0이라서 모든 토마토가 익은 상태(0일 걸림) 와 벽(0 표시) 이 중복되는 문제가 생긴다.

assets/images/2025/7569.jpg



💻 코드

전체 코드

import java.io.*;
import java.util.*;

public class Main {
    static int c, r, h;
    static int[][][] arr;
    static int[][][] v;

    static Deque<Pos> dq;

    static int[] dz = {-1, 1, 0, 0, 0, 0};
    static int[] dx = {0, 0, -1, 1, 0, 0};
    static int[] dy = {0, 0, 0, 0, -1, 1};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        c = Integer.parseInt(st.nextToken());
        r = Integer.parseInt(st.nextToken());
        h = Integer.parseInt(st.nextToken());

        arr = new int[h][r][c];
        v = new int[h][r][c];

        // 1: 익음, 0: 익지않음, -1: 없음
        dq = new ArrayDeque<>();

        for(int z = 0; z < h; z++){
            for(int i = 0; i < r; i++){
                st = new StringTokenizer(br.readLine());
                for(int j=0; j < c; j++){
                    arr[z][i][j] = Integer.parseInt(st.nextToken());

                    if(arr[z][i][j] == 1) {
                        dq.offer(new Pos(z, i, j));
                        v[z][i][j] = 1;
                    }
                }
            }
        }


        bfs();

        int ans = -1;
        for(int z = 0; z < h; z++) {
            for (int i = 0; i < r; i++) {
                for (int j = 0; j < c; j++) {

                    //  익지 않아야 할 토마토가 남았는지 확인 (익어야 할 대상(0)인데 방문 못함(0))
                    if(arr[z][i][j] == 0 && v[z][i][j] == 0){
                        System.out.println(-1);
                        return;
                    }

                    ans = Math.max(ans, v[z][i][j]);
                }
            }
        }
        System.out.println(ans - 1);
    }

    static void bfs() {
        while(!dq.isEmpty()) {
            Pos cur = dq.poll();

            // 3-1. 6방향, 범위
            for(int d = 0; d<6; d++){
                int nz = cur.z + dz[d];
                int nx = cur.x + dx[d];
                int ny = cur.y + dy[d];

                if(nz < 0 || nz >= h || nx < 0 || nx >= r || ny < 0 || ny >= c) continue;

                // 3-2. 익지않음, 미방문
                if(arr[nz][nx][ny] == 0 && v[nz][nx][ny] == 0){
                    v[nz][nx][ny] = v[cur.z][cur.x][cur.y] + 1;
                    dq.offer(new Pos(nz, nx, ny));
                }
            }
        }
    }

    static class Pos{
        int z, x, y;

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


스켈레톤 코드

import java.io.*;
import java.util.*;

public class Main {
    static int[][][] arr;

    static int[] dz = {-1, 1, 0, 0, 0, 0};
    static int[] dx = {0, 0, -1, 1, 0, 0};
    static int[] dy = {0, 0, 0, 0, -1, 1};

    static int col, row, h;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        StringTokenizer st = new StringTokenizer(br.readLine());
        col = Integer.parseInt(st.nextToken());
        row = Integer.parseInt(st.nextToken());
        h = Integer.parseInt(st.nextToken());

        arr = new int[h][row][col];
        Deque<int[]> dq = new ArrayDeque<>();



    }

    static void bfs(Deque<int[]> dq){


    }
}


카테고리:

업데이트:

댓글남기기