天天看點

poj 2251 Dungeon Master(BFS三維)

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20912 Accepted: 8106

點選打開連結

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!      

題意: 

 給出一三維空間的地牢,要求求出由字元'S'到字元'E'的最短路徑

移動方向可以是東、西、南、北、上、下六個個方向

每移動一次就耗費一分鐘,要求輸出最快的走出時間。

不同L層的地圖,相同RC坐标處是連通的

#無法行通

.代表路

題目分析:

  此題與hdoj 1242 Rescue 十分相似

 隻不過這個題稍微複雜的是三維,其實也複雜不了多少,小心就是啦

代碼:

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define MAX 35
char map[MAX][MAX][MAX];
int step[MAX][MAX][MAX];
int L,R,C;
int sx,sy,sz,ex,ey,ez;
int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct node
{
  int x;
  int y;
  int z;
  int time;
};
queue<node>q;

void input()
{
  for(int i=0;i<L;i++)
   for(int j=0;j<R;j++)
     for(int k=0;k<C;k++)
	 {
	   if(map[i][j][k]=='S')
	   {
	    sx=i;
		sy=j;
		sz=k;	
	   }
	   if(map[i][j][k]=='E')
	   {
	    ex=i;
		ey=j;
		ez=k;	
	   }
	   step[i][j][k]=-1;	
	 }	
}

int valid(int x,int y,int z)
{
	if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C)
	 return 1;
	else
	 return 0;
}

void BFS()
{
	node p1,p2;
	p1.x=sx;
	p1.y=sy;
	p1.z=sz;
	p1.time=0;
	step[p1.x][p1.y][p1.z]=0;
	
	while(!q.empty())
	q.pop();
	
	q.push(p1);
	while(!q.empty())
	{
	  p1=q.front();
	  q.pop();
	  
	  for(int i=0;i<6;i++)//注意這兒
	  {
	    p2.x=p1.x+f[i][0];
		p2.y=p1.y+f[i][1];	
		p2.z=p1.z+f[i][2];
    
		if(map[p2.x][p2.y][p2.z]!='#'&&valid(p2.x,p2.y,p2.z))
      	{
      	 p2.time=p1.time+1;
		  if(step[p2.x][p2.y][p2.z]==-1||p2.time<step[p2.x][p2.y][p2.z])
		  {
			q.push(p2);
			step[p2.x][p2.y][p2.z]=p2.time; 
		  }	
		}		
	  } 
	}
}

int main()
{
	while(~scanf("%d%d%d",&L,&R,&C),(L||R||C))
	{
	  for(int i=0;i<L;i++)
	    for(int j=0;j<R;j++)
	      scanf("%s",map[i][j]);
	      input();
	      BFS();
	      if(step[ex][ey][ez]==-1)
	       printf("Trapped!\n");
	     else
	      printf("Escaped in %d minute(s).\n",step[ex][ey][ez]);
	}
	return 0;
}
           

看了人家的解題報告,我發現人家的思路比我的簡練的多了是以我再按照人家的思路再貼一個:

#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define MAX 35
char map[MAX][MAX][MAX];
int vis[MAX][MAX][MAX];
int step[MAX][MAX][MAX];
int L,R,C;
int sx,sy,sz;
int f[][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct node
{
  int x;
  int y;
  int z;
  int time;
};
queue<node>q;

void input()
{
  for(int i=0;i<L;i++)
   for(int j=0;j<R;j++)
     for(int k=0;k<C;k++)
	 {
	   if(map[i][j][k]=='S')
	   {
	    sx=i;
		sy=j;
		sz=k;	
	   }
	   vis[i][j][k]=0;	
	 }	
}

int valid(int x,int y,int z)
{
	if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C)
	 return 1;
	else
	 return 0;
}

void BFS()
{
	node p1,p2;
	p1.x=sx;
	p1.y=sy;
	p1.z=sz;
	p1.time=0;

	while(!q.empty())
	q.pop();
	
	vis[p1.x][p1.y][p1.z]=1;
	q.push(p1);
	while(!q.empty())
	{
	  p1=q.front();
	  q.pop();
	  
	  for(int i=0;i<6;i++)//注意這兒
	  {
	    p2.x=p1.x+f[i][0];
		p2.y=p1.y+f[i][1];	
		p2.z=p1.z+f[i][2];
    
		if(map[p2.x][p2.y][p2.z]!='#'&&valid(p2.x,p2.y,p2.z)&&!vis[p2.x][p2.y][p2.z])
      	{
      	 p2.time=p1.time+1;
		  if(map[p2.x][p2.y][p2.z]=='E')//此處直接傳回值,不用那麼複雜
		  { 
		   printf("Escaped in %d minute(s).\n",p2.time);
		   return ;
		  }	
		 vis[p2.x][p2.y][p2.z]=1;
		 q.push(p2);
		}		
	  } 
	}
	printf("Trapped!\n");
}

int main()
{
	while(~scanf("%d%d%d",&L,&R,&C),(L||R||C))
	{
	  for(int i=0;i<L;i++)
	    for(int j=0;j<R;j++)
	      scanf("%s",map[i][j]);
	      input();
	      BFS();
	}
	return 0;
}