-
Notifications
You must be signed in to change notification settings - Fork 0
/
6593.java
101 lines (78 loc) · 2.21 KB
/
6593.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.*;
import java.util.*;
public class Main {
static int dx[] = {0,0,-1,1,0,0};
static int dy[] = {-1,1,0,0,0,0};
static int dz[] = {0,0,0,0,1,-1};
static int L, R, C;
static char map[][][];
static boolean visit[][][];
static int me[] = null, exit[] = null;
public static void main(String args[]) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
while(true) {
String mapSize[] = input.readLine().split(" ");
L = Integer.parseInt(mapSize[0]);
R = Integer.parseInt(mapSize[1]);
C = Integer.parseInt(mapSize[2]);
if(L == 0 && R == 0 && C == 0) {
break;
}
map = new char[R][C][L];
visit = new boolean[R][C][L];
me = null; exit = null;
for(int l=0; l<L; l++) {
for(int r=0; r<R; r++) {
char row[] = input.readLine().toCharArray();
for(int c=0; c<C; c++) {
map[r][c][l] = row[c];
if(row[c] == 'S') {
me = new int[] {r,c,l};
}else if(row[c] == 'E') {
exit = new int[]{r,c,l};
}
}
}
input.readLine();
}
if(me == null || exit == null) {
output.write("Trapped!\n");
continue;
}
int answer = escape();
output.write(answer < 1 ? "Trapped!\n" : "Escaped in "+answer+" minute(s).\n");
}
output.flush();
}
static int escape() {
Queue<int[]> queue = new LinkedList<>();
queue.add(me);
visit[me[0]][me[1]][me[2]] = true;
int time = 0;
while(!queue.isEmpty()) {
int queueSize = queue.size();
time++;
while(queueSize-- > 0) {
int cur[] = queue.poll();
for(int i=0; i<6; i++) {
int nx = cur[0] + dx[i];
int ny = cur[1] + dy[i];
int nz = cur[2] + dz[i];
if(rangeCheck(nx, ny, nz)) {
if(nx == exit[0] && ny == exit[1] && nz == exit[2]) {
return time;
}
queue.add(new int[] {nx, ny, nz});
visit[nx][ny][nz] = true;
}
}
}
}
return -1;
}
static boolean rangeCheck(int nx, int ny, int nz) {
return nx>=0 && ny>=0 && nz>=0 && nx<R && ny<C && nz<L && !visit[nx][ny][nz] &&
map[nx][ny][nz] != '#' ? true : false;
}
}