Dungeon Master
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 84 Accepted Submission(s) : 30
Problem 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). < br>L is the number of levels making up the dungeon. < br>R and C are the number
of rows and columns making up the plan of each level. < br>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 < br><blockquote>Escaped in x minute(s). </blockquote>< br>where x is replaced by the shortest time it takes to escape. < br>If
it is not possible to escape, print the line < br><blockquote>Trapped! </blockquote>
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’,求最短路径问题。
但是我做的时候路途曲折,先是给他深搜的,好不容易深搜完,也过了样例,卡死在了超时上,想不出来有什么解决方法。所以看来最短路径的问题还是要广搜的,首先时间上广搜就肯定有保证。
下面给出深搜广搜两种代码,作为对比吧。
//深搜 超时代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int l,r,c,mint;
int si,sj,sk;
int dx[6]={1,-1,0,0,0,0};
int dy[6]={0,0,1,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
char mapp[50][50][50];
int vis[50][50][50];
int ok(int x,int y,int z)
{
if(x>0&&x<=l && y>0&&y<=r && z>0&&z<=c)
return 1;
return 0;
}
void ioan(int x,int y,int z,int t)
{
for(int k=0;k<6;k++)
{
int xx=x+dx[k];
int yy=y+dy[k];
int zz=z+dz[k];
if(mapp[xx][yy][zz]=='E')
{
if(mint>t+1)
mint=t+1;
return ;
}
if(ok(xx,yy,zz)&&vis[xx][yy][zz]==0)
{
if(mapp[xx][yy][zz]=='.')
{
vis[xx][yy][zz]=1;
ioan(xx,yy,zz,t+1);
vis[xx][yy][zz]=0;
}
}
}
return ;
}
int main()
{
//ios::sync_with_stdio(false);
char ch;
//freopen("D:\\aaa.txt","r",stdin);
while(scanf("%d %d %d",&l,&r,&c))
{
if((l+r+c)==0)
break;
mint=1e9;
memset(vis,0,sizeof(vis));
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
vis[i][j][k]=0;
cin>>mapp[i][j][k];
if(mapp[i][j][k]=='S')
{
si=i; sj=j; sk=k;
}
}
}
}
vis[si][sj][sk]=1;
ioan(si,sj,sk,0);
if(mint<1000)
printf("Escaped in %d minute(s).\n",mint);
//cout<<"Escaped in "<<mint<<" minute(s)."<<endl;
else
printf("Trapped!\n");
//cout<<"Trapped!"<<endl;
}
return 0;
}
//广搜 AC代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int l,r,c;
int si,sj,sk,ei,ej,ek,flag;
int dx[6]={1,-1,0,0,0,0};
int dy[6]={0,0,1,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
int mapp[50][50][50];//地图
int vis[50][50][50];//是否走过
struct Node
{
int x,y,z,t;
};
int ioan(int x,int y,int z)
{
int head=0,tail=1;
Node a;
Node p[100010];
memset(p,0,sizeof(p));
a.x=x;
a.y=y;
a.z=z;
a.t=0;
vis[a.x][a.y][a.z]=1;
p[tail]=a;
do
{
head++;
a=p[head];
for(int k=0;k<6;k++)
{
Node b=a;
b.x=a.x+dx[k];
b.y=a.y+dy[k];
b.z=a.z+dz[k];
if(b.x==ei && b.y==ej && b.z==ek)
{
flag=1;
return (a.t+1);
}
if(mapp[b.x][b.y][b.z]==1&&vis[b.x][b.y][b.z]==0)//没有越界 是路 没有走过
{
vis[b.x][b.y][b.z]=1;
b.t=a.t+1;
tail++;
p[tail]=b;
}
}
}while(head<tail);
}
int main()
{
char ch;
//freopen("D:\\aaa.txt","r",stdin);
while(cin>>l>>r>>c)
{
if((l+r+c)==0)
break;
flag=0;
memset(mapp,0,sizeof(mapp));
memset(vis,0,sizeof(vis));
for(int i=1;i<=l;i++)
{
for(int j=1;j<=r;j++)
{
for(int k=1;k<=c;k++)
{
cin>>ch;
if(ch=='.')
{
mapp[i][j][k]=1;
}
else if(ch=='S')
{
si=i; sj=j; sk=k;
}
else if(ch=='E')
{
mapp[i][j][k]=1;
ei=i; ej=j; ek=k;
}
}
}
}
vis[si][sj][sk]=1;
int time=ioan(si,sj,sk);
if(flag==1)
//printf("Escaped in %d minute(s).\n",time);
cout<<"Escaped in "<<time<<" minute(s)."<<endl;
else
//printf("Trapped!\n");
cout<<"Trapped!"<<endl;
}
return 0;
}