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;
    }
  • 相关阅读:
    js动态添加、删除行
    java内存问题排查及分析
    IDE 热部署配置
    Target JRE version (1.7.0_79) does not match project JDK version (java version "1.8.0_171"), will use sources from JDK: 1.7
    快速排序
    归并排序思想
    算法总结
    百度2013校园招聘笔试题(含整理的答案)
    浙江省委书记(习):加快推进节约型社会建设
    申论方面的经验
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215111.html
Copyright © 2011-2022 走看看