Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 46   Accepted Submission(s) : 16
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?
 
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!
 
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
char map[31][31][31];
int vis[31][31][31];
int dx[6]={1,0,-1,0,0,0};
int dy[6]={0,0,0,1,0,-1};
int dz[6]={0,1,0,0,-1,0};
int x,y,z,ex,ey,ez,m,n,l;
struct node
{
int x,y,z;
int step;
friend bool operator< (node n1,node n2)
{
return n1.step>n2.step;
}
}p,temp;
bool judge(node r)
{
if(r.x<0||r.x>=m||r.y<0||r.y>=n||r.z<0||r.z>=l)
return true;
if(vis[r.x][r.y][r.z]||map[r.x][r.y][r.z]=='#')
return true;
return false;
}
int bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node>q;
while(!q.empty()) q.pop();
p.x=x;
p.y=y;
p.z=z;
p.step=0;
q.push(p);
vis[x][y][z]=1;
while(!q.empty())
{
p=q.top();
q.pop();
if(p.x==ex&&p.y==ey&&p.z==ez)
{
return p.step;
}
for(int i=0;i<6;i++)
{
temp=p;
temp.x=p.x+dx[i];
temp.y=p.y+dy[i];
temp.z=p.z+dz[i];
if(judge(temp))
continue;
vis[temp.x][temp.y][temp.z]=1;
temp.step=p.step+1;
q.push(temp);
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&m,&n,&l)!=EOF)
{
memset(map,'\0',sizeof(map));
getchar();
if(m+n+l==0)
break;
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%s",&map[i][j]);
for(k=0;k<l;k++)
{ if(map[i][j][k]=='S')
{
x=i;y=j;z=k;
}
if(map[i][j][k]=='E')
{
ex=i;ey=j;ez=k;
}
}
//getchar();
}
/*for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
for(k=0;k<l;k++)
printf("%c",map[i][j][k]);
printf("\n");
}*/
//printf("%d %d %d %d %d% d",x,y,z,ex,ey,ez);
int ans=bfs();
if(ans!=0)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}

最新文章

  1. 使用Logstash进行日志分析
  2. 自动插入数据sql
  3. iOS7上TableViewCell的button和UIImageView个别未显示的bug
  4. 五个JS经典面试题
  5. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)
  6. Exploit搭建
  7. Java 7 Fork/Join 并行计算框架概览
  8. JS中setAttribute的兼容性问题(摘自leejersey)
  9. 使用Battery Historian(android 5.0)
  10. Nodejs 项目开发
  11. diff命令
  12. 【转】javascript Object使用Array的方法
  13. SAP RFC介绍:关于sRFC,aRFC,tRFC,qRFC和bgRFC
  14. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.coder520.mamabike.user.dao.UserMapper.selectByPrimaryKey
  15. OC中NSString的使用、字符串的使用
  16. java基础-3
  17. mysql加速source导入数据
  18. 安装Elasticsearch中Head插件并使用
  19. JS从数组中随机取出几个数组元素的方法
  20. 可以在任何时候attach一个shader到program对象

热门文章

  1. 使用composer 实现自动加载
  2. Windows下使用Caffe-Resnet
  3. Matlab/Eigen矩阵填充问题
  4. 实验0 安装GLUT包及工程的创建与运行
  5. 【Python基础】条件语句
  6. luoguP4719 【模板】动态 DP 线段树+树链剖分+矩阵乘法+动态DP
  7. PAT_A1151#LCA in a Binary Tree
  8. HDU1087 - Super Jumping! Jumping! Jumping!【动态规划】
  9. iptables 实现内网转发上网
  10. js中window.location的用法