Question:
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?
简单翻译:给一个三维的迷宫(立体空间当中的),给出起点和终点的三维坐标,求出从起点到终点的最短距离。
Input:
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.
Output:
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! "
Sample Input:
3 4 5
S…
.###.
.##…
###.#
##.##
##…
#.###
####E1 3 3
S##
#E#
0 0 0
Sample Output:
Escaped in 11 minute(s).
Trapped!
AC codes:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=30;
char map [maxn][maxn][maxn]; //map[z][x][y];
bool marked[maxn][maxn][maxn];
int L,R,C;
struct Dir
{
int l;
int r;
int c;
}move[]={{1,0,0},
{-1,0,0},
{0,1,0},
{0,-1,0},
{0,0,1},
{0,0,-1}
};
//描述方向的时候不用结构化数组,用二维数组也可以,即:
//int dir[6][3]={{1,0,0},......}
struct Node
{
int r;
int c;
int l;
int step;
}now,next,start,end;
//queue<Node> q;
//
//void clear(queue<Node>& q) {
// queue<Node> empty;
// swap(empty, q); //队列并没有.clear()方法,如果要用的话要自己定义
//}
bool stop(int x, int y, int z) //row, column, level
{
if(map[z][x][y]=='#') return true;
else if(marked[z][x][y]) return true;
else if(z<0||z>=L) return true;
else if(x<0||x>=R) return true;
else if(y<0||y>=C) return true;
return false;
}
void bfs()
{
queue<Node> q;
q.push(start);
while(!q.empty()){
now = q.front();
q.pop();
if(now.l==end.l&&now.r==end.r&&now.c==end.c)
{
cout<<"Escaped in "<<now.step<<" minute(s)."<<endl;
return;
}
int i;
for(i=0; i<6; i++)
{
next.c=now.c+move[i].c;
next.l=now.l+move[i].l;
next.r=now.r+move[i].r;
next.step = now.step + 1;
if(!stop(next.r, next.c, next.l))
{
q.push(next);
marked[next.l][next.r][next.c]=true; //一开始wa就是因为忘记标记了
}
}
}
cout<<"Trapped!"<<endl;
}
int main()
{
int x,y,z;
while(cin>>L>>R>>C)
{
if(!L && !R && !C) return 0;
for(z=0; z<L; z++) //L
{
for(x=0; x<R; x++) //R
{
for(y=0; y<C; y++) //C
{
char ch;
cin>>ch;
if(ch=='S')
{
start.c = y;
start.r = x;
start.l = z;
start.step = 0;
marked[z][x][y]=true;
}
if(ch=='E')
{
end.c = y;
end.r = x;
end.l = z;
}
map[z][x][y] = ch;
}
cin.get(); //吞掉换行符
}
cin.get(); //按照题目要求每一层之间输入有一个空行
}
bfs();
memset(marked, 0, sizeof(marked));
}
}