广搜
只不过这道题目需要搜索六个方向,即:上、下、左、右、上一层、下一层
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct list
{
int l;
int r;
int c;
int step;
};
bool check(int x,int y,int z);
int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
//上、下、左、右、上一层、下一层 l r c
char an[31][31][31];
bool book[31][31][31];
int l,r,c;
int main()
{
while(scanf("%d%d%d",&l,&r,&c),l||r||c)
{
memset(an,0,sizeof(an));
memset(book,false,sizeof(book));
struct list n,term;
for(int i=0;i<l;i++)
{
for(int j=0;j<r;j++)
{
for(int k=0;k<c;k++)
{
scanf(" %c",&an[i][j][k]);
if(an[i][j][k]=='S')
{
n.l=i;
n.r=j;
n.c=k;
}
if(an[i][j][k]=='E')
{
term.l=i;
term.r=j;
term.c=k;
}
}
}
}
n.step=0;
book[n.l][n.r][n.c]=true;
queue<struct list> q;
q.push(n);
while(!(q.front().l==term.l&&q.front().r==term.r&&q.front().c==term.c)&&!q.empty())
{
for(int i=0;i<6;i++)
{
int x,y,z;
x=q.front().l+dir[i][2];
y=q.front().r+dir[i][0];
z=q.front().c+dir[i][1];
if(check(x,y,z))
{
book[x][y][z]=true;
n.l=x;
n.r=y;
n.c=z;
n.step=q.front().step+1;
q.push(n);
}
}
q.pop();
}
if(q.empty())
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",q.front().step);
}
return 0;
}
bool check(int x,int y,int z)
{
if(x<l&&x>=0&&y<r&&y>=0&&z<c&&z>=0)
if(book[x][y][z]==false)
if(an[x][y][z]=='.'||an[x][y][z]=='E')
return true;
return false;
}