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....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
这是一道三维的广度优先搜索的题目,flag一下~
#include
#include
#include
#define MAXN 44
int c, k, h;
char ma[MAXN][MAXN][MAXN];
int visit[MAXN][MAXN][MAXN];
struct node
{
int c, k, h;//结构体记录到达某个点
int step;//走的步数
};
struct node t[33433];//结构体队列
struct node p, q, w, l;
int f[][3] = {0,0,1, 0,0,-1, 0,1,0, 0,-1,0, 1,0,0, -1,0,0};//向上下左右前后六个方向移动
void bfs()
{
visit[p.c][p.k][p.h] = 1;//标记已经访问过了
int front = 0, rear = 0;
t[rear++] = p;//入队
while(front!=rear)//当队列不为空的时候
{
w = t[front++];
if(w.c==q.c&&w.k==q.k&&w.h==q.h)//如果是终点,输出
{
printf("Escaped in %d minute(s).\n", w.step);
return ;
}
for(int i=0;i<6;i++)//否则,遍历六个方向
{
l = w;
l.c += f[i][0];
l.k += f[i][1];
l.h += f[i][2];
if(l.c>=0&&l.c=0&&l.k=0&&l.h'#'&&visit[l.c][l.k][l.h]==0)//如果符合条件
{
l.step++;//步数增加
visit[l.c][l.k][l.h] = 1;//标记访问过了
t[rear++] = l;//入列
}
}
}
printf("Trapped!\n");//否则,输出无法到达
}
int main()
{
memset(t, 0, sizeof(struct node));
while(~scanf("%d %d %d", &c, &k, &h)&&c&&k&&h)//c长k宽h高
{
getchar();
for(int i=0;ifor(int j=0;jscanf("%s", ma[i][j]);//按照字符串输入
for(int w = 0;wif(ma[i][j][w]=='S')//记录起点
{
p.c = i;
p.k = j;
p.h = w;
p.step = 0;
}
else if(ma[i][j][w]=='E')//记录终点
{
q.c = i;
q.k = j;
q.h = w;
}
}
}
}
memset(visit, 0, sizeof(visit));//清空标记数组
bfs();//广度优先搜索
}
return 0;
}