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;
    }
  • 相关阅读:
    刘墉写给女儿的忠告
    HTML & CSS 小总结
    [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
    [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
    [LeetCode] 127. Word Ladder 单词阶梯
    [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
    [LeetCode] 354. Russian Doll Envelopes 俄罗斯套娃信封
    [LeetCode] 218. The Skyline Problem 天际线问题
    [LeetCode] 407. Trapping Rain Water II 收集雨水 II
    [LeetCode] 309. Best Time to Buy and Sell Stock with Cooldown 买卖股票的最佳时间有冷却期
  • 原文地址:https://www.cnblogs.com/zxy160/p/7215111.html
Copyright © 2011-2022 走看看