Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18557 | Accepted: 7216 |
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!
解题思路:
乍一看,挺难,看了看,三维最短路问题可以转化为二维最短路问题,二维最短路我会做哈,然后直接套了进去,AC了本道题,求最短路想都不用想,肯定是BFS最好用了,网上还有用DFS做的,感觉没有BFS方便,贴一下代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 40;
const int maxn1= 30000;
char map[maxn][maxn][maxn]; //分别代表第i个level,第j行,第c列
int dir[6][3] = {{0,0,1},{1,0,0},{0,1,0},{0,0,-1},{-1,0,0},{0,-1,0}};
bool vis[maxn][maxn][maxn];
int m,n,k;
int length[maxn1];
struct point
{
int x,y,z;
}q[30000];
int BFS(int s1,int s2,int s3,int e1,int e2,int e3) //分别代表起点和终点的坐标
{
int head,tail;
int i;
int dx,dy,dz;
head = tail = 0;
q[0].x = s1;
q[0].y = s2;
q[0].z = s3;
while(head <= tail)
{
for(i=0;i<6;i++)
{
dx = q[head].x + dir[i][0];
dy = q[head].y + dir[i][1];
dz = q[head].z + dir[i][2];
if(dx >= 0 && dy >= 0 && dz >=0 && dx < m && dy < n && dz < k && vis[dx][dy][dz] == false && (map[dx][dy][dz] == '.' || map[dx][dy][dz] == 'E'))
{
q[++tail].x = dx;
q[tail].y = dy;
q[tail].z = dz;
vis[dx][dy][dz] = true;
length[tail] = length[head] + 1;
if(dx == e1 && dy == e2 && dz == e3)
{
return length[tail];
}
}
}
head++;
}
return 0;
}
int main()
{
int i,j,c;
//freopen("111","r",stdin);
while(cin>>m>>n>>k && m && n && k)
{
int pos1,pos2,pos3,pos4,pos5,pos6;
memset(vis,false,sizeof(vis));
memset(length,0,sizeof(length));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(c=0;c<k;c++)
{
cin>>map[i][j][c];
if(map[i][j][c] == 'S')
{
pos1 = i;
pos2 = j;
pos3 = c;
}
if(map[i][j][c] == 'E')
{
pos4 = i;
pos5 = j;
pos6 = c;
}
}
}
}
int res = BFS(pos1,pos2,pos3,pos4,pos5,pos6);
if(res != 0)
cout<<"Escaped in "<<res<<" "<<"minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}