1. 程式人生 > >3Ddungeon ~ 3D迷宮 BFS

3Ddungeon ~ 3D迷宮 BFS

題目描述

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? 

輸入

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.

輸出

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!

樣例輸入

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

樣例輸出

Escaped in 11 minute(s).
Trapped!

提示

本題的意思其實就是一個3D迷宮,相對與2d 迷宮就多了一個z,

思路bsf

dfs應該會超時;

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
struct Node
{
	int x , y, z, step;
	Node() {} 
	Node(int xx,int yy,int zz,int ss) : x(xx) , y (yy) , z(zz) , step(ss) {}
};
int sx,sy,sz,ex,ey,ez;
const int dx[6] = { 0,0,1,0,-1,0 };
const int dy[6] = { 0,0,0,1,0,-1 };
const int dz[6] = { -1,1,0,0,0,0 };
char a[35][35][35];
int book[35][35][35];
int n, m ,l ;
int bfs(int x,int y,int z,int step)
{
	queue<Node> Q;
	Q.push(Node(x,y,z,step));
	while(!Q.empty())
	{
		Node u = Q.front() ;
		Q.pop();
		if(u.x == ex && u.y == ey && u.z == ez)
		{
			return u.step ;
		} 
		int nowx,nowy,nowz,nows;
		for(int i = 0 ; i < 6 ; i++)
		{
			nowx = u.x + dx[i] ;
			nowy = u.y + dy[i] ;
			nowz = u.z + dz[i] ;
			nows = u.step + 1 ;
			if(nowx>=0 && nowx < n && nowy >= 0 && nowy < m && nowz >= 0 && nowz < l&&a[nowx][nowy][nowz] != '#' &&book[nowx][nowy][nowz] == 0 )
			{
				book[nowx][nowy][nowz] = 1 ;
			//	a[nowx][nowy][nowz] = '#';
				Q.push(Node(nowx,nowy,nowz,nows)) ; 
				
			}
		}
 	}
 	return -1 ; 
}
int main()
{
	while(cin>>n>>m>>l)
	{
		memset(a,0,sizeof(a));
		memset(book,0,sizeof(book));
		if(n==0 && m==0 && l == 0)
		{
			break;
		}
		for(int i=0 ; i < n ; i++)
		{
			for(int j=0 ; j < m ; j++)
			{
				for(int k=0 ; k < l ; k++)
				{
					cin>>a[i][j][k];
					if(a[i][j][k] == 'S')
					{
						sx=i;
						sy=j;
						sz=k;
					}
					if(a[i][j][k] == 'E')
					{
						ex=i;
						ey=j;
						ez=k;
					}
					/*if(a[i][j][k] == '#')
					{
						book[i][j][k] = 1;
					}*/
					
				}
			}
		}
		book[sx][sy][sz] = 1;
		int ans=bfs(sx,sy,sz,0);
		if(ans==-1)
		{
			cout<<"Trapped!"<<endl;
		}
		else
		{
			printf("Escaped in %d minute(s).",ans);
			cout<<endl;
		}
	} 
	
}