POJ 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!


题目意思给定 L个 R*C的矩阵,每次移动坐标或者进入上一个或下一个矩阵,时间消耗一分钟,试问能否在L个矩阵里找到从S找到E,若能请输出Escaped in %d minute(s). %d为最短时间,否则输出Trapped!,



见到最短时间又是迷宫,首选bfs解法,
#include <iostream>
#include <queue>
using namespace std;
int l,r,c;
char maze[30][30][30];

struct node
{
	int l,y,x,m;
}s,ex;

struct dir   //对应六种状态
{
	int c,y,x;
}d[6];

void bfs()
{
     queue<node> p;	
     p.push(s);
	 while(!p.empty())
	 {
	    node t=p.front();   //   提取队首 
	    p.pop();
	 
	 	for(int i=0;i<6;i++)
	 	{ 
	 		node t1=t;      // 以队首状态的进入下一状态 
	     	t1.l+=d[i].c;
	     	t1.y+=d[i].y;
	     	t1.x+=d[i].x;
	     		
	        if(t1.l<l&&t1.l>=0 && t1.y<r&&t1.y>=0 && t1.x<c&&t1.x>=0  &&  maze[t1.l][t1.y][t1.x]!='#' )
	 		{
			 		t1.m++;
	 			if(t1.l==ex.l&&t1.y==ex.y&&t1.x==ex.x)
	 			{
	 				printf("Escaped in %d minute(s).\n",t1.m);
	 				
	 				return ;
				}
				maze[t1.l][t1.y][t1.x]='#' ;    //注意标记,这里的标记代替了vis的功能
	 		
	 			p.push(t1);
			 }
		 }
      }	
      
      	printf("Trapped!\n");
	
}
int main()
{
	d[0].c=0;d[0].y=0;d[0].x=1;           //初始化六种状态 
		d[1].c=0;d[1].y=0;d[1].x=-1;
			d[2].c=0;d[2].y=1;d[2].x=0;
				d[3].c=0;d[3].y=-1;d[3].x=0;
					d[4].c=1;d[4].y=0;d[4].x=0;
				    	 d[5].c=-1;d[5].y=0;d[5].x=0;
				    	 
				    	 
   while(cin>>l>>r>>c&&l!=0)	
   {
   for(int i=0;i<l;i++)
	 for(int j=0;j<r;j++)
	  for(int k=0;k<c;k++)
	    {
	    	cin>>maze[i][j][k];
	    	if(maze[i][j][k]=='S')  //记录S的坐标 
	    	{
	    	   s.l=i;s.y=j;s.x=k;
			}
			
			  if(maze[i][j][k]=='E')  //记录E的坐标 
	    	{
	    		ex.l=i;ex.y=j;ex.x=k;
			}
		}
		
    bfs();
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值