zoukankan      html  css  js  c++  java
  • NOI2.5 1253:Dungeon Master

    描述

    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!

     

    题目大意:

    给定一个三维迷宫,给出起点(S)和终点(T),问从起点到终点要走几分钟(走一步一分钟),如果走不到终点,输出“Trapped!”

    三维?!你提莫的在逗我?!二维已经够呛了,你给我来三维!!!

    细想一下,也不是很难,广搜的话,多加两个方向的走法就可以了呀

    队列再加一个来保存层数就行了呀

    伪队列写法,一次AC

     

    代码如下:

    <span style="font-size:12px;BACKGROUND-COLOR: #ffff99">#include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    using namespace std;
    char c[35];
    int f[6]={1,-1,0,0,0,0},u[6]={0,0,-1,1,0,0},g[6]={0,0,0,0,-1,1},l,m,n,xz,yz,zz,num,nn;
    bool v[35][35][35];
    void find(int p,int q,int r)
    {
    	int x=0,y=0,z=0,t=0,w=1,i;
    	int h[100001][4];
    	h[1][0]=p;
    	h[1][1]=q;
    	h[1][2]=r;
    	h[1][3]=0;
    	do
    	{
    		t++;
    		for(i=0;i<6;i++)
    		{
    			x=h[t][0]+f[i];
    			y=h[t][1]+u[i];
    			z=h[t][2]+g[i];
    			if(x>=0&&x<m&&y>=0&&y<n&&z>=0&&z<l&&!v[x][y][z])
    			{
    				w++;
    				h[w][0]=x;
    				h[w][1]=y;
    				h[w][2]=z;
    				h[w][3]=h[t][3]+1;
    				v[x][y][z]=1;
    				if(x==xz&&y==yz&&z==zz)
    				{
    					num=h[w][3];
    					return ;
    				}
    			}
    		}
    	}while(t<w);
    }
    int main()
    {
    	int i,j,x,y,z;
    	scanf("%d%d%d",&l,&m,&n);
    	while(l&&m&&n)
    	{
    		num=0;
    		for(int o=0;o<l;o++)
    		for(i=0;i<m;i++)
    		{
    			scanf("%s",c);
    			for(j=0;j<=n;j++)
    			{
    				if(c[j]=='#')
    					v[i][j][o]=1;
    				if(c[j]=='S')
    				{
    					x=i;
    					y=j;
    					z=o;
    					v[i][j][o]=0;
    				}
    				if(c[j]=='E')
    				{
    					xz=i;
    					yz=j;
    					zz=o;
    					v[i][j][o]=0;
    				}
    				if(c[j]=='.')
    					v[i][j][o]=0;
    			}
    		}
    		v[x][y][z]=1;
    		find(x,y,z);
    		if(num)
    			printf("Escaped in %d minute(s).
    ",num);
    		else
    			printf("Trapped!
    ");
    		scanf("%d%d%d",&l,&m,&n);
    	}
    }</span>
    <span style="font-size:12px;BACKGROUND-COLOR: #ffff99"></span> 

    伪队列的方法很节约时间,即使三维也不会很耗时,所以,就这样啦

  • 相关阅读:
    httpclient的并发连接问题
    Java中使用多线程、curl及代理IP模拟post提交和get访问
    有强大的cURL,忘掉httpclient的吧!
    JavaScript正则表达式在不同浏览器中可能遇到的问题
    Meta对照表
    IntelliJ IDEA 下的svn配置及使用的非常详细的图文总结
    虚拟机centos7服务器下,启动oracle11g数据库和关闭数据库
    Step 4: Install and Configure Databases
    docker 中部署一个springBoot项目
    docker保存对容器的修改
  • 原文地址:https://www.cnblogs.com/Darknesses/p/12002586.html
Copyright © 2011-2022 走看看