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

    Dungeon Master
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 33157 Accepted: 12711
    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!

    第二次做,找了一个小时bug,生无可恋
    题意:三维迷宫找出口
    错误点:一:步数不是now.teap++,而是now.teap+1;
    二:定义清0是#define mem(a,b) memset(a,b,sizeof(a))

    #include <stdio.h>
    #include<string.h>
    #include <queue>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define mem(a,b) memset(a,b,sizeof(a))
    char a[35][35][35];
    int vis[35][35][35];
    int sh,si,sj;
    int eh,ei,ej;
    int h,n,m;
    int to[6][3]=
    {
        1,0,0,
        -1,0,0,
        0,1,0,
        0,-1,0,
        0,0,1,
        0,0,-1
    };
    struct node
    {
        int hh,ii,jj;
        int step;
    };
    int bfs(int sh,int si,int sj)
    {
        mem(vis,0);
        vis[sh][si][sj]=1;
        node now,next;
        queue<node>s;
        now.hh=sh,now.ii=si,now.jj=sj,now.step=0;
        s.push(now);
        while(!s.empty())
        {
            now=s.front();
            if(now.hh==eh&&now.ii==ei&&now.jj==ej)
            {
                return now.step;
                break;
            }
            for(int i=0; i<6; i++)
            {
                int H=now.hh+to[i][0],X=now.ii+to[i][1],Y=now.jj+to[i][2];
                if(H>=0&&H<h&&X>=0&&X<n&&Y>=0&&Y<m&&a[H][X][Y]!='#'&&!vis[H][X][Y])
                {
                    vis[H][X][Y]=1;
                    next.hh=H,next.ii=X,next.jj=Y,next.step=now.step+1;
                    s.push(next);
                }
            }
            s.pop();
        }
        if(s.empty()) return -1;
    }
    int main()
    {
        while(~scanf("%d%d%d",&h,&n,&m)&&(h||n||m))
        {
            mem(a,0);
            for(int k=0; k<h; k++)
            {
    
                for(int i=0; i<n; i++)
                {
                    scanf("%s",a[k][i]);
                    for(int j=0; j<m; j++)
                    {
                        if(a[k][i][j]=='S')
                            sh=k,si=i,sj=j;
                        if(a[k][i][j]=='E')
                            eh=k,ei=i,ej=j;
                    }
                }
            }
            int ans=bfs(sh,si,sj);
            if(ans>0)
                printf("Escaped in %d minute(s).
    ",ans);
            else
                printf("Trapped!
    ");
        }
        return 0;
    }
  • 相关阅读:
    使用库项目实现Android程序代码的复用 java程序员
    编译触摸tslib出错:libtool: link: only absolute runpaths are allowed java程序员
    Linux下的TFTP软件的安装设置(TQ210)ubuntu11.10 java程序员
    拥有超过1200个PNG格式图标的免费图标集 Cosmo Mini java程序员
    Android Develop Training中文翻译06《Starting Another Activity》 java程序员
    周记二
    周记三
    周记
    周记
    周记
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215111.html
Copyright © 2011-2022 走看看