zoukankan      html  css  js  c++  java
  • Dungeon Master hdoj

                                     Dungeon Master

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 46   Accepted Submission(s) : 16
    Problem 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!
     

    #include<stdio.h>
    #include<queue>
    #include<string.h>
    using namespace std;
    char map[31][31][31];
    int vis[31][31][31];
    int dx[6]={1,0,-1,0,0,0};
    int dy[6]={0,0,0,1,0,-1};
    int dz[6]={0,1,0,0,-1,0};
    int x,y,z,ex,ey,ez,m,n,l;
    struct node 
    {
    	int x,y,z;
    	int step;
    	friend bool operator< (node n1,node n2)
    	{
    		return n1.step>n2.step;
    	}
    }p,temp;
    bool judge(node r)
    {
    	if(r.x<0||r.x>=m||r.y<0||r.y>=n||r.z<0||r.z>=l)
    	return true;
    	if(vis[r.x][r.y][r.z]||map[r.x][r.y][r.z]=='#')
    	return true;
    	return false;
    }
    int bfs()
    {
    	memset(vis,0,sizeof(vis));
    	priority_queue<node>q;
    	while(!q.empty()) q.pop();
    	p.x=x;
    	p.y=y;
    	p.z=z;
    	p.step=0;
    	q.push(p);
    	vis[x][y][z]=1;
    	while(!q.empty())
    	{
    		p=q.top();
    		q.pop();
    		if(p.x==ex&&p.y==ey&&p.z==ez)
    		{
    			return p.step;
    		}
    		for(int i=0;i<6;i++)
    		{
    			temp=p;
    			temp.x=p.x+dx[i];
    			temp.y=p.y+dy[i];
    			temp.z=p.z+dz[i];
    			if(judge(temp))
    			continue;
    			vis[temp.x][temp.y][temp.z]=1;
    			temp.step=p.step+1;
    			q.push(temp);
    		}
    	}
    	return 0;
    }
    int main()
    {
    	while(scanf("%d%d%d",&m,&n,&l)!=EOF)
    	{
    		memset(map,'',sizeof(map));
    		getchar();
    		if(m+n+l==0)
    		break;
    		int i,j,k;
    		for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    		{
    			scanf("%s",&map[i][j]);
    			for(k=0;k<l;k++)
    			{
    			
    			if(map[i][j][k]=='S')
    			{
    				x=i;y=j;z=k;
    			}
    			if(map[i][j][k]=='E')
    			{
    				ex=i;ey=j;ez=k;
    			}
    			}
    		//getchar();
    		}
    		/*for(i=0;i<m;i++)
    		for(j=0;j<n;j++)
    		{
    			for(k=0;k<l;k++)
    			printf("%c",map[i][j][k]);
    			printf("
    ");
    		}*/
    		//printf("%d %d %d %d %d% d",x,y,z,ex,ey,ez);
    		int ans=bfs();
    		if(ans!=0)
    		printf("Escaped in %d minute(s).
    ",ans);
    		else 
    		printf("Trapped!
    ");
    	}
    	return 0;
    }
     

  • 相关阅读:
    JavaScript实现继承机制(4)——构造函数+原型链混合方式
    NodeJS”热部署“代码,实现动态调试
    初识NodeJS,一个基于GoogleV8引擎的Javascript运行环境
    那些你不得不知道的JavaScript 变量命名规则
    JavaScript声明全局变量的三种方式
    JavaScript实现继承机制(3)——通过原型链(prototype chaining)方式
    JavaScript实现继承机制(1)—— 构造函数方法对象冒充
    C# readonly和const
    C# winform增加界面动态加载的流畅性
    C# 正确操作字符串,规避字符串转换所带来的额外开销
  • 原文地址:https://www.cnblogs.com/playboy307/p/5273854.html
Copyright © 2011-2022 走看看