(三维广搜)3Ddungeon

119 篇文章 1 订阅
113 篇文章 1 订阅

3Ddungeon

时间限制: 1000ms
内存限制: 128000KB
64位整型:      Java 类名:
类型: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                   
  • 题目描述

    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>
    using namespace std;
    char map[35][35][35];
    int xia[35][35][35];
    int go[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//走的方向;
    struct gg
    {
        int x,y,z,time;
    };
    queue<gg>q;
    int x2,y2,z2;
    void ff(int xa,int ya,int za)
    {
        gg n1;
        int flag=1;
        n1.x=xa,n1.y=ya,n1.z=za,n1.time=0;
        q.push(n1);
        xia[xa][ya][za]=1;
        while(!q.empty())
        {
            n1=q.front();
            if(map[n1.x][n1.y][n1.z]=='E')
            {
                flag=0;
                break;
            }
            q.pop();
            for(int i=0; i<6; i++)
            {
                int xx=n1.x+go[i][1],yy=n1.y+go[i][0],zz=n1.z+go[i][2];
                if(xx>=0&&xx<x2&&yy>=0&&yy<y2&&zz>=0&&zz<z2&&xia[xx][yy][zz]==0)
                {
                    if(map[xx][yy][zz]!='#')
                    {
                        gg n2;
                        xia[xx][yy][zz]=1;
                        n2.x=xx,n2.y=yy,n2.z=zz,n2.time=n1.time+1;
                        q.push(n2);
                    }
                }
            }
        }
        if(flag)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",n1.time);
        while(!q.empty())
            q.pop();
        return ;
    }
    int main()
    {
        int xa,ya,za;
        while(~scanf("%d%d%d",&x2,&y2,&z2),x2||y2||z2)//注意x2,y2,z2的位置,不要写反;
        {
            memset(map,0,sizeof(map));
            memset(xia,0,sizeof(xia));
            int i,j,k;
            for(i=0; i<x2; i++)
            {
                for(j=0; j<y2; j++)
                {
                    scanf("%s",map[i][j]);
                    for(k=0; k<z2; k++)//找到开始的位置
                        if(map[i][j][k]=='S')
                            xa=i,ya=j,za=k;//记录开始的位置
                }
            }
            ff(xa,ya,za);
        }
        return 0;
    }
    


    第二种要小心了;(我找了半天快疯了)

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    struct gg
    {
        int x,y,z,step;
    };
    char map[35][35][35];
    int go[6][3]= {0,1,0,
                   0,-1,0,
                   0,0,1,
                   0,0,-1,
                   1,0,0,
                   -1,0,0
                  };
    int x1,y1,z1;
    queue<gg>q;
    void ff(int x2,int y2,int z2)
    {
        gg n1;
        n1.x=x2,n1.y=y2,n1.z=z2,n1.step=0;
        q.push(n1);
        map[x2][y2][z2]='#';
        while(!q.empty())
        {
            n1=q.front();
            if(map[n1.x][n1.y][n1.z]=='E')
                break;
            q.pop();
            for(int i=0; i<6; i++)
            {
                int xx=n1.x+go[i][1],yy=n1.y+go[i][0],zz=n1.z+go[i][2];
                if(xx>=0&&xx<x1&&yy>=0&&yy<y1&&zz>=0&&zz<z1)
                {
                    if(map[xx][yy][zz]!='#')
                    {
                        gg n2;
                        n2.x=xx,n2.y=yy,n2.z=zz,n2.step=n1.step+1;
                        if(map[xx][yy][zz]!='E')//注意:不要把目标也弄成#,否则无法判断是否到达目标
                            map[xx][yy][zz]='#';
                        q.push(n2);
                    }
                }
            }
        }
        if(q.empty())
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",n1.step);
        while(!q.empty())
            q.pop();
        return ;
    }
    int main()
    {
        while(~scanf("%d%d%d",&x1,&y1,&z1),x1||y1||z1)
        {
            memset(map,0,sizeof(map));
            int i,j,xx,yy,zz;
            for(i=0; i<x1; i++)
            {
                for(j=0; j<y1; j++)
                {
                    scanf("%s",map[i][j]);
                    for(int h=0; h<z1; h++)
                    {
                        if(map[i][j][h]=='S')
                            xx=i,yy=j,zz=h;
                    }
                }
            }
            ff(xx,yy,zz);
        }
        return 0;
    }


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值