Dungeon Master
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? 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! Source |
题意:给出一个三维图,#代表墙,.代表能走的路,求S到E的最短路径。
解题思路:在别人博客里面的题目分类看到了这一题,不过被归在了DFS算法里面,读完题后才发现这是个BFS题。六个方向,(0,0,1)(0,0,-1)(-1,0,0)(1,0,0)(0,-1,0)(0,1,0),剩下的套用BFS模板就行了。在边界问题跪了无数次。。。下面是我的AC代码
#include <iostream>
#include <cstdio>
#include <memory.h>
#include <queue>
using namespace std;
#define maxn 35
char pic[maxn][maxn][maxn];
int flag[maxn][maxn][maxn];
int ex,ey,ez,success;
int l,h,w;
int mm[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};
struct trip
{
int x;
int y;
int z;
int step;
};
int check(int p,int r,int s)
{
if(p<0||p>=l||r<0||r>=h||s<0||s>=w||pic[p][r][s]=='#') return 1;
return flag[p][r][s];
}
void bfs(int p,int r,int s)
{
trip a,next;
queue<trip> q;
a.x=p;
a.y=r;
a.z=s;
a.step=0;
flag[p][r][s]=1;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
{
printf("Escaped in %d minute(s).\n",a.step);
success=1;
break;
}
for(int i=0;i<6;i++)
{
next.x=a.x+mm[i][0];
next.y=a.y+mm[i][1];
next.z=a.z+mm[i][2];
if(check(next.x,next.y,next.z)) continue;
next.step=a.step+1;
flag[next.x][next.y][next.z]=1;
q.push(next);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&l,&h,&w))
{
if(l==0&&h==0&&w==0) return 0;
success=0;
for(int i=0; i<l; i++)
for(int j=0; j<h; j++)
scanf("%s",pic[i][j]);
for(int i=0; i<l; i++)
for(int j=0; j<h; j++)
for(int k=0; k<w; k++)
if(pic[i][j][k]=='E')
{
ex=i;
ey=j;
ez=k;
break;
}
memset(flag,0,sizeof(flag));
for(int i=0; i<l; i++)
for(int j=0; j<h; j++)
for(int k=0; k<w; k++)
if(pic[i][j][k]=='S') bfs(i,j,k);
if(success==0) printf("Trapped!\n");
}
return 0;
}