zoukankan      html  css  js  c++  java
  • HDOJ-三部曲一(搜索、数学)-1005-Dungeon Master

    Dungeon Master

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 18   Accepted Submission(s) : 12
    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!
     
    Source
    PKU
     
     
     
    又一道BFS水题,我发现我刷搜索水题很溜啊哈哈。。
     
     
    #include<iostream>
    #include<cstring>
    using namespace std;
    char cube[31][31][31];
    bool sign[31][31][31];  
    int L,R,C;
    
    struct pos
    {
    	int l,r,c;
    };
    
    pos que[54000],beg;
    int step[54000];
    int BFS()
    {
    	int front=0,rear=1;
    	que[0]=beg;
    	sign[que[0].l][que[0].r][que[0].c]=true;
    	while(front<rear)
    	{
    		if(que[front].l-1>=0&&!sign[que[front].l-1][que[front].r][que[front].c]&&cube[que[front].l-1][que[front].r][que[front].c]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].l=que[front].l-1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		if(que[front].l+1<L&&!sign[que[front].l+1][que[front].r][que[front].c]&&cube[que[front].l+1][que[front].r][que[front].c]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].l=que[front].l+1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		if(que[front].r-1>=0&&!sign[que[front].l][que[front].r-1][que[front].c]&&cube[que[front].l][que[front].r-1][que[front].c]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].r=que[front].r-1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		if(que[front].r+1<R&&!sign[que[front].l][que[front].r+1][que[front].c]&&cube[que[front].l][que[front].r+1][que[front].c]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].r=que[front].r+1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		if(que[front].c-1>=0&&!sign[que[front].l][que[front].r][que[front].c-1]&&cube[que[front].l][que[front].r][que[front].c-1]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].c=que[front].c-1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		if(que[front].c+1<C&&!sign[que[front].l][que[front].r][que[front].c+1]&&cube[que[front].l][que[front].r][que[front].c+1]!='#')
    		{
    			que[rear]=que[front];
    			que[rear].c=que[front].c+1;
    			sign[que[rear].l][que[rear].r][que[rear].c]=true;
    			step[rear]=step[front]+1;
    			if(cube[que[rear].l][que[rear].r][que[rear].c]=='E')
    				return step[rear];
    			rear++;
    		}
    		front++;
    	}
    	return -1;
    }
    
    
    int main()
    {
    	while(cin>>L>>R>>C&&(R+C+L))
    	{
    		int i,j,k;
    		memset(sign,false,sizeof(sign));
    		memset(step,0,sizeof(step));
    		for(i=0;i<L;i++)
    		{
    			for(j=0;j<R;j++)
    				for(k=0;k<C;k++)
    				{
    					cin>>cube[i][j][k];
    					if(cube[i][j][k]=='S')
    					{
    						beg.l=i;
    						beg.r=j;
    						beg.c=k;
    					}
    				}
    		}
    		int ans=BFS();
    		if(ans==-1)
    			cout<<"Trapped!"<<endl;
    		else
    			cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
    
    	}
    }
    
     
  • 相关阅读:
    洛谷P4113 [HEOI2012]采花
    洛谷P5159 WD与矩阵
    洛谷P1262 间谍网络
    洛谷P3038 牧草种植Grass Planting
    洛谷P3258 [JLOI2014]松鼠的新家
    洛谷P2294 [HNOI2005]狡猾的商人
    洛谷P4878 [USACO05DEC]layout布局
    【CF1132F】Clear the String (DP)
    [AH2017/HNOI2017]大佬(动态规划 搜索)
    「NOI2018」屠龙勇士(CRT)
  • 原文地址:https://www.cnblogs.com/aljxy/p/3337585.html
Copyright © 2011-2022 走看看