코테/백준

[백준/JAVA] 7569번: 토마토

imname1am 2023. 7. 21. 16:37
반응형

🔺 문제

 

7569번: 토마토

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

www.acmicpc.net

 

 

🔺 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.*;
import java.io.*;
 
class tomato {
    int x, y, z;    // 세로, 가로, 면
    
    public tomato(int z, int x, int y) {
        this.z = z;
        this.x = x;
        this.y = y;
    }
}
 
public class Main {
    static int[] dx = {00-1100};  // 상하좌우 앞뒤
    static int[] dy = {1-10000};
    static int[] dz = {00001-1};
    
    static int M,N,H;
    static int[][][] map;
    
    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());   // 높이
 
        map = new int[H][N][M];
        queue = new LinkedList<>();
            
        for(int k = 0 ; k < H ; k++) {
            for(int i = 0 ; i < N ; i++) {
                st = new StringTokenizer(br.readLine(), " ");
                for(int j = 0 ; j < M ; j++) {
                    map[k][i][j] = Integer.parseInt(st.nextToken());
                    
                    // 🔔 익은 토마토면, 큐에 넣음 🔔
                    if(map[k][i][j] == 1)
                        queue.add(new tomato(k, i, j));
                }
            }
        }
        
        System.out.println(bfs());
    }
    
    
    static int bfs() {
        while(!queue.isEmpty()) {
            tomato t = queue.poll();
            
            for(int d = 0 ; d < 6 ; d++) {
                int x = t.x + dx[d];
                int y = t.y + dy[d];
                int z = t.z + dz[d];
                
                if(x < 0 || y < 0 || z < 0 || x >= N || y >= M || z >= H || map[z][x][y] != 0)  continue;
                queue.add(new tomato(z, x, y));         // 익은 토마토 추가
                map[z][x][y] = map[t.z][t.x][t.y] + 1;  // ⭐ 이동한 노드 값 = 이전 노드 값 + 1
            }
        }
        
        int result = Integer.MIN_VALUE;
        
        for(int k = 0 ; k < H ; k++) {
            for(int i = 0 ; i < N ; i++) {
                for(int j = 0 ; j < M ; j++) {
                    // 안 익은 토마토가 있는 경우
                    if(map[k][i][j] == 0)   return -1;
                    
                    // 토마토가 익는 데 걸리는 시간 계산
                    result = Math.max(result, map[k][i][j]);
                }
            }
        }
        
        if(result == 1return 0;           // 토마토가 모두 익은 상태인 경우
        else            return result - 1;  // ⭐ 걸린 일수는 최대 날짜 - 1
    }
}
 
cs
✅ 해결 아이디어
✔ BFS
- 그런데 여기에 3차원을 곁들인.. (6방향 bfs 탐색)

 

 


💬 느낀 점

풀이를 보면...

엄청엄청 어려운 건 아닌데..

아직 혼자 생각해내는 힘이 부족허다..

 

분발!!!!

 

 

1회독 2회독 3회독 4회독 5회독
V        

(참고)

 

[백준 알고리즘] 백준 7569번 토마토 자바(Java)

츄르사려고 코딩하는 코집사입니다. 1. [백준 알고리즘] 백준 7569번 토마토 자바(Java) 1) 문제번호 : 7569번 2) 문제 출처 www.acmicpc.net/problem/7569 7569번: 토마토 첫 줄에는 상자의 크기를 나타내는 두

yongku.tistory.com

 

[백준 7569 : JAVA] 토마토 / BFS

개요 이 문제는 아래의 문제에서 배열만 3차원 배열로 변경된 문제이다. 아래 문제를 풀고 이 문제를 접근하면 쉽게 풀이할 수 있을 것이다. 2020/02/09 - [알고리즘/백준] - [백준 7576 : JAVA] 토마토 /

dragon-h.tistory.com

 

 

반응형