zoukankan      html  css  js  c++  java
  • Dungeon Master ZOJ 1940【优先队列+广搜】

    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!
     

    //一次AC。感觉太爽了。!


    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    char map[40][40][40];
    int x1,y1,z1,x2,y2,z2;
    int dx[]={0,1,0,-1,0,0};
    int dy[]={1,0,-1,0,0,0};
    int dz[]={0,0,0,0,1,-1};
    int n,row,column;
    struct Dungeon//地牢 
    {
    	int x,y,z;
    	int time;
    	friend bool operator < (Dungeon a,Dungeon b)
    	{
    		return a.time>b.time;
    	}
    };
    
    bool judge(int xt,int yt,int zt)
    {
    	if(xt>row||xt<1||yt>column||yt<1||zt>n||zt<1) //越界 
    		return 0;
    	if(map[zt][xt][yt]=='#') 
    		return 0;
    	return 1;
    }
    int BFS()
    {
    	priority_queue<Dungeon>q;
    	Dungeon pos,next;
    	pos.x=x1;
    	pos.y=y1;
    	pos.z=z1;
    	pos.time=0;
    	map[z1][x1][y1]='#';
    	q.push(pos);
    	while(!q.empty())
    	{
    		pos=q.top();
    		q.pop();
    		for(int i=0;i<6;++i)
    		{
    			next.x=pos.x+dx[i];
    			next.y=pos.y+dy[i];
    			next.z=pos.z+dz[i];
    			next.time=pos.time+1;
    			if(judge(next.x,next.y,next.z))
    			{
    				if(next.x==x2&&next.y==y2&&next.z==z2)
    				{
    					return next.time;
    				}
    				map[next.z][next.x][next.y]='#';
    				q.push(next);
    			}
    		}
    	}
    	return -1;
    }
    int main()
    {
    
    	while(~scanf("%d%d%d",&n,&row,&column),n+row+column)
    	{
    		getchar();
    		for(int i=1;i<=n;++i)
    		{
    			for(int j=1;j<=row;++j)
    			{
    				for(int k=1;k<=column;++k)
    				{
    					scanf("%c",map[i][j]+k);
    					if(map[i][j][k]=='S')
    					{
    						z1=i,x1=j,y1=k;
    					}
    					else if(map[i][j][k]=='E')
    					{
    						z2=i,x2=j,y2=k;
    					}
    				}
    				getchar();
    			}
    			getchar();
    		}
    		int res;
    		res=BFS();
    		if(res==-1) printf("Trapped!
    ");
    		else printf("Escaped in %d minute(s).
    ",res);
    	}
    	return 0;
    }

  • 相关阅读:
    stm8s103 EEPROM烧程序时能否保留
    NEC芯片特别说明
    pic中断特别说明
    删除排序链表中的重复元素 II
    被围绕的区域
    计数二进制子串
    简单工厂模式
    打家劫舍 II
    打家劫舍
    相同的树
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6805319.html
Copyright © 2011-2022 走看看