nyoj 353 3D dungeon
3D dungeon
时间限制:1000 ms | 内存限制:65535 KB
难度:2
描述
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
输入
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
输出
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
样例输入
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int MAXN = 35; const int INF = 0xfffffff; struct Node { int x, y, z; int step; Node() { x = y = z = 0; step = 0; } }; Node TargetPos; char Graph[MAXN][MAXN][MAXN]; //int MinTime[MAXN][MAXN][MAXN]; int row, col, level; int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; bool Is_CanGo(Node CurPos) { if(CurPos.x < 0 || CurPos.x >= level || CurPos.y < 0 || CurPos.y >= row || CurPos.z < 0 || CurPos.z >= col || Graph[CurPos.x][CurPos.y][CurPos.z] == '#') return false; return true; } int BFS(Node S) { queue <Node> Que; Que.push(S); while(!Que.empty()) { Node Curpos = Que.front(); Que.pop(); if(Curpos.x == TargetPos.x && Curpos.y == TargetPos.y && Curpos.z == TargetPos.z) return Curpos.step; for(int i = 0; i < 6; ++i) { Node t; t.x = Curpos.x + dir[i][0]; t.y = Curpos.y + dir[i][1]; t.z = Curpos.z + dir[i][2]; t.step = Curpos.step + 1; if(Is_CanGo(t)) { Que.push(t); Graph[t.x][t.y][t.z] = '#'; } } } return -1; } int main() { while(scanf("%d %d %d", &level, &row, &col) && (level + row + col)) { Node StartPos; for(int i = 0; i < level; ++i) { for(int j = 0; j < row; ++j) { scanf("%s", Graph[i][j]); for(int z = 0; z < col; ++z) { if(Graph[i][j][z] == 'S') StartPos.x = i, StartPos.y = j, StartPos.z = z; else if(Graph[i][j][z] == 'E') TargetPos.x = i, TargetPos.y = j, TargetPos.z = z; } getchar(); } if(i != level - 1) getchar(); } StartPos.step = 0; int t; t = BFS( StartPos ); if(t != -1) printf("Escaped in %d minute(s).\n", t); else printf("Trapped!\n"); } return 0; }