NYOJ-353

3D dungeon
時間限制:1000 ms  |  內存限制:65535 KB
難度:2
描述
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!

解題思路:bfs 搜索

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
	int x,y,z,t;
	friend bool operator<(node a,node b)
	{
		return a.t>b.t;
	} 
};
int L,R,C,sx,sy,sz,vis[35][35][35];
char maze[35][35][35];
int dx[6]={1,0,0,-1,0,0};
int dy[6]={0,1,-1,0,0,0};
int dz[6]={0,0,0,0,-1,1};
int bfs() 
{
	int i;
	memset(vis,0,sizeof(vis));
	priority_queue<node>q;
	node s,p;
	p.x=sx;p.y=sy;p.y=sy;p.z=sz;
	p.t=0;
	vis[sx][sy][sz]=1; 
	q.push(p);
	while(!q.empty())
	{
		p=q.top();
		q.pop();
		if(maze[p.x][p.y][p.z]=='E')  //走到終點,返回所用時間. 
		{
			return p.t;
		}
		for(i=0;i<6;i++)             //上下左右前後六個方向擴展. 
		{
			s.x=p.x+dx[i];
			s.y=p.y+dy[i];
			s.z=p.z+dz[i];
			if(s.x<0||s.x>=L)  continue;     //判斷是否越界 
			if(s.y<0||s.y>=R)  continue;
			if(s.z<0||s.z>=C)  continue;
			if(!vis[s.x][s.y][s.z]&&maze[s.x][s.y][s.z]!='#')   //該位置未走過且不是石頭.則走到該位置 
			{
				vis[s.x][s.y][s.z]=1; 
				s.t=p.t+1;
				q.push(s);
			}
		}
	}
	return -1;
}
int main()
{
	int i,j,k,flag;
	while(scanf("%d%d%d",&L,&R,&C)&&(L|R|C)) 
	{
		flag=0;
		memset(maze,0,sizeof(maze));
	    getchar(); 
		for(i=0;i<L;i++)
		{
			for(j=0;j<R;j++)
			{
				scanf("%s",&maze[i][j]); 
			    for(k=0;k<C;k++)
				{
					
					if(maze[i][j][k]=='S')
					{
						sx=i;sy=j;sz=k;
					} 
				}
				getchar(); 
			}
			getchar(); 
		}
		int t=bfs();
		if(t!=-1) 
		 printf("Escaped in %d minute(s).\n",t);
		else 
		 printf("Trapped!\n"); 
	}
	return 0;
}  


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