Description
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?
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.
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
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!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
题意:给定一个三维迷宫,注意有上下前后左右四个方向,求S到E最少需要多少步!
细节:
注意数据的输入,依次代表 z,x,y;采用广搜,先搜到的即最小,采用深搜会超时!!!!!!
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include <iomanip>
#define maxn 15
#define MAXN 100005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define exp 1e-6
#define pi acos(-1.0)
using namespace std;
struct Point //定义点的结构体
{
int x,y,z;
int s;
}queue1[200001],f,e;
char map1[32][32][32]; //地图
bool mark[32][32][32]; //用来标记是否走过
int r,c,l;
int dx[6]={-1, 0, 1, 0, 0, 0};//六个方向
int dy[6]={0, -1, 0, 1, 0, 0};
int dz[6]={0, 0, 0, 0, 1, -1};
bool go(int a,int b,int cc) //判断是否在界内
{
if(a>=0&&a<c&&b>=0&&b<r&&cc>=0&&cc<l)
return true;
else
return false;
}
int bfs() //深搜
{
int head=0,tail=0; //头指针和尾指针
queue1[head].x=f.x;
queue1[head].y=f.y;
queue1[head].z=f.z;
queue1[head].s=0;
mark[f.x][f.y][f.z]=1;
while(head<=tail) //循环条件
{
int i,j;
for(i=0;i<6;i++)
{
int prev_x = queue1[head].x + dx[i];
int prev_y = queue1[head].y + dy[i];
int prev_z = queue1[head].z + dz[i];
if (go(prev_x, prev_y, prev_z) && !mark[prev_z][prev_y][prev_x] && map1[prev_z][prev_y][prev_x] != '#')
{
queue1[++tail].x = prev_x;
queue1[tail].y = prev_y;
queue1[tail].z = prev_z;
queue1[tail].s= (queue1[head].s + 1);
mark[prev_z][prev_y][prev_x] = 1;
if (queue1[tail].x == e.x && queue1[tail].y == e.y && queue1[tail].z == e.z)
return queue1[head].s+1;
}
}
head++;
}
return -1; //返回-1表示未能出来
}
int main()
{
// freopen("D:\\a.txt","r",stdin);
ios::sync_with_stdio(false);
while(cin>>l>>r>>c&&l+r+c)
{
memset(map1, 0, sizeof (map1));
memset(mark, 0, sizeof (mark));
for (int i = 0; i<l; i++) //此处小技巧 少用一个for循环,节省时间在z ,x
{
for (int j = 0; j <r; j++)
{
cin>>map1[i][j];
}
}
for (int i = 0; i < l; i++)
for (int j = 0; j < r; j++)
for (int k = 0; k < c; k++)
{
if (map1[i][j][k] == 'S') //起始点
{
f.x = k;
f.y = j;
f.z = i;
}
if (map1[i][j][k] == 'E') //终点
{
e.x = k;
e.y = j;
e.z = i;
}
}
int ans = bfs();
if (ans == -1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}