Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27195 | Accepted: 10668 |
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!
Source
题意:求出最短的路径从起点到达终点,不过给出的是三维的。
分析:稍微转换一下即可。没什么东西感觉,给出AC代码:
//三维bfs
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=31;
int map[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int sx,sy,sz,ex,ey,ez;
int c,a,b;
int dirx[6]= {-1,1,0,0,0,0};
int diry[6]= {0,0,-1,1,0,0};
int dirz[6]= {0,0,0,0,-1,1};
struct node
{
int x,y,z,step;
};
int BFS()
{
queue<node>q;
node p1;
p1.x=sx;
p1.y=sy;
p1.z=sz;
vis[sx][sy][sz]=1;
p1.step=0;
q.push(p1);
//printf("%d %d %d\n",p1.x,p1.y,p1.z);
while(!q.empty())
{
node f=q.front(),temp;
q.pop();
if(f.x==ex&&f.y==ey&&f.z==ez)
return f.step;
for(int i=0; i<6; i++)
{
temp.x=f.x+dirx[i];
temp.y=f.y+diry[i];
temp.z=f.z+dirz[i];
temp.step=f.step+1;
if(temp.x<a&&temp.x>=0&&temp.y<b&&temp.y>=0&&temp.z<c&&temp.z>=0&&
!vis[temp.x][temp.y][temp.z]&&(map[temp.x][temp.y][temp.z]==1))
{
q.push(temp);
// printf("%d %d %d\n",temp.x,temp.y,temp.z);
vis[temp.x][temp.y][temp.z]=1;
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&c,&a,&b),a,b,c)
{
char temp;
for(int i=0; i<c; i++)//平面坐标
for(int j=0; j<a; j++)//j是平面x
for(int k=0; k<b; k++)//k是平面y
{
vis[j][k][i]=0;
cin>>temp;
if(temp=='.')
map[j][k][i]=1;
else if(temp=='#')
map[j][k][i]=0;
else if(temp=='S')
{
sx=j;
sy=k;
sz=i;
map[j][k][i]=1;
}
else if(temp=='E')
{
ex=j;
ey=k;
ez=i;
map[j][k][i]=1;
}
}
int res=BFS();
if(res==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",res);
}
return 0;
}
特记下,以备后日回顾。