zoukankan      html  css  js  c++  java
  • nyoj 353 3D dungeon

    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!

    还是感觉广搜比深搜简单点
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    #define MAX 35
    using namespace std;
    int n,m,k;
    int x1,x2,y1,y2,z1,z2;
    char map[MAX][MAX][MAX];
    int vis[MAX][MAX][MAX];
    struct node
    {
    	int x,y,z,step;
    	friend bool operator < (node a,node b)
    	{
    		return a.step>b.step;
    	}
    };
    void bfs()
    {
    	int i,j;
    	int move[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};
    	priority_queue<node>q;
    	node beg,end;
    	beg.x=x1;
    	beg.y=y1;
    	beg.z=z1;
    	beg.step=0;
    	q.push(beg);
    	vis[x1][y1][z1]=1;
    	while(!q.empty())
    	{
    		end=q.top();
    		q.pop();
    		if(end.x==x2&&end.y==y2&&end.z==z2)
    		{
    			printf("Escaped in %d minute(s).
    ",end.step);
    			return ;
    		}
    		for(i=0;i<6;i++)
    		{
    			beg.x=end.x+move[i][0];
    			beg.y=end.y+move[i][1];
    			beg.z=end.z+move[i][2];
    			if(!vis[beg.x][beg.y][beg.z]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&beg.z>=0&&beg.z<k&&map[beg.x][beg.y][beg.z]!='#')
    			{
    				map[beg.x][beg.y][beg.z]='#';
    				beg.step=end.step+1;
    				q.push(beg);
    			}
    		}
    	}
    	printf("Trapped!
    ");
    }
    int main()
    {
    	int i,j,t,s;
    	while(scanf("%d%d%d",&n,&m,&k)&&n!=0&&m!=0&&k!=0)
    	{
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    			{
    				scanf("%s",map[i][j]);
    			}
    		}	
    		for(i=0;i<n;i++)
    		{
    			for(j=0;j<m;j++)
    			{
    				for(t=0;t<k;t++)
    				{
    					if(map[i][j][t]=='S')
    					{
    						x1=i;y1=j;z1=t;
    					}
    					if(map[i][j][t]=='E')
    					{
    						x2=i;y2=j;z2=t;
    					}
    				}
    			}
    		}
    		memset(vis,0,sizeof(vis));
    		bfs();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    CentOS7防火墙
    [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
    二叉树系列
    二叉树系列
    [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
    动态规划小结
    [LeetCode] Populating Next Right Pointers in Each Node I, II
    [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
    二叉树系列
    [LeetCode] 数组的最长连续数, O(n)解法
  • 原文地址:https://www.cnblogs.com/tonghao/p/4622009.html
Copyright © 2011-2022 走看看