-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjungol_2606.java
79 lines (72 loc) · 1.84 KB
/
jungol_2606.java
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
package com.ssafy.algo;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class jungol_2606 {
static int N, M, H;
static final int dx[] = { 0, 0, 0, 0, 1, -1 };
static final int dy[] = { 0, 0, -1, 1, 0, 0 };
static final int[] dz = { -1, 1, 0, 0, 0, 0 }; // 위아래상하좌우
static int[][][] tomato;
static class Tomato {
int x;
int y;
int z;
public Tomato(int z, int x, int y) {
this.z = z;
this.x = x;
this.y = y;
}
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
// data input
M = sc.nextInt();
N = sc.nextInt();
H = sc.nextInt();
tomato = new int[H][N][M];
Queue<Tomato> 토마토큐 = new LinkedList<>();
for (int i = 0; i < H; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < M; k++) {
tomato[i][j][k] = sc.nextInt();
if (tomato[i][j][k] == 1) {
토마토큐.add(new Tomato(i, j, k));
}
}
}
}
// solve
while (!토마토큐.isEmpty()) {
Tomato cur = 토마토큐.poll();
int tX = cur.x;
int tY = cur.y;
int tZ = cur.z;
for (int i = 0; i < dx.length; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
int nz = cur.z + dz[i];
if (nz < 0 || nx < 0 || ny < 0 || nz >= H || nx >= N || ny >= M || tomato[nz][nx][ny] != 0) {
continue;
}
tomato[nz][nx][ny] = tomato[tZ][tX][tY] + 1;
토마토큐.add(new Tomato(nz, nx, ny));
}
}
// find max value
int res = 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 (tomato[i][j][k] == 0) {
System.out.println("-1");
return;
}
res = Math.max(res, tomato[i][j][k]);
}
}
}
// data output
System.out.println((res - 1) + "");
}
}