zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master

    来源:http://poj.org/problem?id=2251



    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16522   Accepted: 6425

    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


    PS: BFS---标准走迷宫模板题

    代码:
    #include<iostream>
    #include<string>
    using namespace std;
    const int Max=30000;
    int L,R,C,xe,ye,ze;
    bool visit[31][30][30];
    string str[31][31];
    int dir[6][3]={
        {0,0,1},{0,0,-1},
        {-1,0,0},{1,0,0},
        {0,1,0},{0,-1,0}
    };
    struct Node{
        int xs,ys,zs,time;
        Node()
        {
        xs=0;ys=0;zs=0;time=0;
        };
    } map[Max],temp;
    
    int main()
    {
        while((cin>>L>>R>>C)&&(L||R||C))
        {
        int i,j,k,flag=1;
        for(i=0;i<L;i++){
        for(j=0;j<R;j++)
        {
        cin>>str[i][j];
        for(k=0;k<C;k++)
        if(str[i][j][k]=='.')
        visit[i][j][k]=true;
        else if(str[i][j][k]=='#')
        visit[i][j][k]=false;
        else if(str[i][j][k]=='S')
        {
         map[0].xs=i;   map[0].ys=j;    map[0].zs=k; visit[i][j][k]=false;
        }
        else if(str[i][j][k]=='E')
        {
        xe=i;    ye=j;ze=k; visit[i][j][k]=true;
        }
        }
        }
        int nodedir=0,exdir=0;
        while(nodedir<=exdir&&exdir<Max&&flag)
        {
            for(int i=0;i<6;i++)
            {
            temp=map[nodedir];  //设置父节点 
            int tx=temp.xs+dir[i][0],ty=temp.ys+dir[i][1],tz=temp.zs+dir[i][2];
                if(tx>=0&&tx<L&&ty>=0&&ty<R&&tz>=0&&tz<C&&visit[tx][ty][tz])
                {
                temp.xs=tx; temp.ys=ty; temp.zs=tz;
                visit[tx][ty][tz]=false;
                temp.time++;
                map[++exdir]=temp;
                if(temp.xs==xe&&temp.ys==ye&&temp.zs==ze)
                {
                cout<<"Escaped in "<<temp.time<<" minute(s)."<<endl;
                flag=0;
                break;
                }
                }
            }
            nodedir++;
        }
        if(flag) cout<<"Trapped!"<<endl;
        }
        return 0;
    }





  • 相关阅读:
    c#创建对象并动态添加属性
    js从$scope外部调用$scope内部函数,跨js调用非全局函数
    JQuery中$.ajax()方法参数详解
    c#关于int(或其他类型)的字段在对象初始化时默认初始化问题的解决方法
    SQLServer中存储过程StoredProcedure创建及C#调用(转)
    2020年将热门的8大IT职业领域
    2015总结+2016计划
    hadoop程序在本地模式调试作业
    Flume+Kafka+storm的连接整合
    scp 和 pscp
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6903038.html
Copyright © 2011-2022 走看看