zoukankan      html  css  js  c++  java
  • C Dungeon Master

    C - Dungeon Master

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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!

    //用dfs做的,就是在试探的时候多试探一次上下位置的时候就行。
    #include <iostream>
    #include <queue>
    #include <string.h>
    using namespace std;
    
    struct point
    {
        int x,y,h;
        int step;
    };
    point star,end;
    
    char map [35][35][35];
    bool way [35][35][35];
    int h,x,y;
    
    void read_map()//读地图
    {
        for (int i=1;i<=h;i++)
        {
            for (int j=1;j<=x;j++)
            {
                for (int k=1;k<=y;k++)
                {
                    cin>>map[i][j][k];
                    if (map[i][j][k]=='S')
                    {
                        star.h=i;
                        star.x=j;
                        star.y=k;
                        star.step=0;
                    }
                    if (map[i][j][k]=='E')
                    {
                        end.h=i;
                        end.x=j;
                        end.y=k;
                        
                    }
                }
            }
        }
    }
    
    int check(point t)//检查是否能走
    {
    
    
        if (  t.x>=1 && t.x<=x && t.y>=1 && t.y<=y && t.h>=1 && t.h<=h && way[t.h][t.x][t.y]==0 )
        {
            if ( map[t.h][t.x][t.y]!='#')
            {
            return 1;
            }
        }
        return 0;
    }
    
    
    int bfs()
    {
        point now,next;
        int min=-1;
        
        queue<point> Q;
        Q.push(star);
        way[star.h][star.x][star.y]=1;
    
        while (!Q.empty())
        {
            now=Q.front();
            Q.pop();
            if (now.x==end.x&&now.y==end.y&&now.h==end.h)
            {
                min=now.step;
                break;
            }
    
            next.x=now.x+1;
            next.y=now.y;
            next.h=now.h;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
    
            next.x=now.x;
            next.y=now.y-1;
            next.h=now.h;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
    
            next.x=now.x-1;
            next.y=now.y;
            next.h=now.h;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
    
            next.x=now.x;
            next.y=now.y+1;
            next.h=now.h;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
    
            next.x=now.x;
            next.y=now.y;
            next.h=now.h+1;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
    
            next.x=now.x;
            next.y=now.y;
            next.h=now.h-1;
            next.step=now.step+1;
            if (check(next)){ Q.push(next); way[next.h][next.x][next.y]=1;}
        }
        return min;
        
    }
    
    
    int main()
    {
        int all;
        while (cin>>h>>x>>y)
        {
            memset(way,0,sizeof(way));
            if (h==0&&x==0&&y==0) break;
            read_map();
            all=bfs();
            if (all==-1)
                cout<<"Trapped!"<<endl;
            else
            cout<<"Escaped in "<<all<<" minute(s)."<<endl;
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    TensorFlow 基础 (04)
    面向对象编程思想的介绍
    B2B、B2C、C2C、O2O的概念
    为什么我们需要域
    如何在阿里云服务器上搭建wordpress个人网站
    Ghost手动备份、还原系统详细图文教程
    IE浏览器下载文件保存时提示:“你没有权限在此位置中保存文件”解决办法
    电脑经常自动重启的一些解决办法
    ERP系统到底能做什么?
    SQL实用技巧:如何分割字符串
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5676672.html
Copyright © 2011-2022 走看看