This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ7569.java
More file actions
119 lines (97 loc) · 3.27 KB
/
Copy pathBOJ7569.java
File metadata and controls
119 lines (97 loc) · 3.27 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BOJ7569 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int h = sc.nextInt();
int[][][] map = new int[h + 2][n + 2][m + 2];
for (int i = 0; i <= h + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
Arrays.fill(map[i][j], -1);
}
}
Queue<Integer> xQueue = new LinkedList<>();
Queue<Integer> yQueue = new LinkedList<>();
Queue<Integer> zQueue = new LinkedList<>();
int count = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= m; k++) {
map[i][j][k] = sc.nextInt();
if (map[i][j][k] > 0) {
xQueue.add(i);
yQueue.add(j);
zQueue.add(k);
}
if (map[i][j][k] == 0) {
count++;
}
}
}
}
int day = 0;
while (true) {
if (count == 0) {
System.out.println(day);
return;
}
if (count > 0 && xQueue.isEmpty()) {
System.out.println(-1);
return;
}
day++;
int queueSize = xQueue.size();
for (int i = 0; i < queueSize; i++) {
int x = xQueue.remove();
int y = yQueue.remove();
int z = zQueue.remove();
if (map[x][y - 1][z] == 0) {
xQueue.add(x);
yQueue.add(y - 1);
zQueue.add(z);
map[x][y - 1][z] = 1;
count--;
}
if (map[x][y + 1][z] == 0) {
xQueue.add(x);
yQueue.add(y + 1);
zQueue.add(z);
map[x][y + 1][z] = 1;
count--;
}
if (map[x - 1][y][z] == 0) {
xQueue.add(x - 1);
yQueue.add(y);
zQueue.add(z);
map[x - 1][y][z] = 1;
count--;
}
if (map[x + 1][y][z] == 0) {
xQueue.add(x + 1);
yQueue.add(y);
zQueue.add(z);
map[x + 1][y][z] = 1;
count--;
}
if (map[x][y][z + 1] == 0) {
xQueue.add(x);
yQueue.add(y);
zQueue.add(z + 1);
map[x][y][z + 1] = 1;
count--;
}
if (map[x][y][z - 1] == 0) {
xQueue.add(x);
yQueue.add(y);
zQueue.add(z - 1);
map[x][y][z - 1] = 1;
count--;
}
}
}
}
}