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();
   }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章