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

     

  • 相关阅读:
    java占位符应用
    【QuickHit项目实例】
    【那些年关于java多态应用】
    【那些年关于MyEclipse的快捷键大全】
    那些年【深入.NET平台和C#编程】
    关于《网络电视精灵》项目
    VS2013常用快捷键
    关于C#的继承结论
    关于【项目经理评分】项目的代码分析
    序列化和发序列化
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5676672.html
Copyright © 2011-2022 走看看