zoukankan      html  css  js  c++  java
  • POJ

    Dungeon Master

    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!



    之前做过类似的A计划,那道题是二维传送地图,当时是用DFS写的。这次的Dungeon Master是A计划的升级版,达到30的多维地图(WA:只能传送到相邻维度。。),所以尝试DFS果断超时,,然后重码了一遍BFS-_-16msA掉。这再一次地证明了在处理最短路径时BFS的效率之高。


    DFS TLE代码:
    #include<stdio.h>
    #include<string.h>
    
    char a[35][35][35];
    int b[35][35][35];
    int t[6][3]={{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{1,0,0},{-1,0,0}};
    int ww,n,m,bw,bx,by,min;
    
    void dfs(int w,int x,int y,int s)
    {
        int i;
        if(w<0||x<0||y<0||w>=ww||x>=n||y>=m) return;
        if(a[w][x][y]=='E'){
            if(s<min) min=s;
            return;
        }
        if(a[w][x][y]=='#'||b[w][x][y]==1) return;
        for(i=0;i<6;i++){
            int tw=w+t[i][0];
            int tx=x+t[i][1];
            int ty=y+t[i][2];
            b[w][x][y]=1;
            dfs(tw,tx,ty,s+1);
            b[w][x][y]=0;
        }
    }
    
    int main()
    {
        int i,j,k;
        while(scanf("%d%d%d",&ww,&n,&m)&&!(ww==0&&n==0&&m==0)){
            for(i=0;i<ww;i++){
                for(j=0;j<n;j++){
                    getchar();
                    scanf("%s",a[i][j]);
                    for(k=0;k<m;k++){
                        if(a[i][j][k]=='S'){
                            bw=i;
                            bx=j;
                            by=k;
                        }
                    }
                }
            }
            min=1000000000;
            memset(b,0,sizeof(b));
            dfs(bw,bx,by,0);
            if(min==1000000000) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ",min);
        }
        return 0;
    }

    BFS AC代码:

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    using namespace std;
    
    char a[35][35][35];
    int b[35][35][35];
    int t[6][3]={{0,1,0},{0,0,1},{0,-1,0},{0,0,-1},{1,0,0},{-1,0,0}};
    
    struct Node{
        int w,x,y,s;
    }node;
    
    int main()
    {
        int ww,n,m,i,j,k;
        while(scanf("%d%d%d",&ww,&n,&m)&&!(ww==0&&n==0&&m==0)){
            queue<Node> q;
            memset(b,0,sizeof(b));
            for(i=0;i<ww;i++){
                for(j=0;j<n;j++){
                    getchar();
                    scanf("%s",a[i][j]);
                    for(k=0;k<m;k++){
                        if(a[i][j][k]=='S'){
                            b[i][j][k]=1;
                            node.w=i;
                            node.x=j;
                            node.y=k;
                            node.s=0;
                            q.push(node);
                        }
                    }
                }
            }
            int f=0;
            while(q.size()){
                for(i=0;i<6;i++){
                    int tw=q.front().w+t[i][0];
                    int tx=q.front().x+t[i][1];
                    int ty=q.front().y+t[i][2];
                    if(tw<0||tx<0||ty<0||tw>=ww||tx>=n||ty>=m) continue;
                    if(a[tw][tx][ty]=='E'){
                        f=q.front().s+1;
                        break;
                    }
                    if(a[tw][tx][ty]=='#'||b[tw][tx][ty]==1) continue;
                    b[tw][tx][ty]=1;
                    node.w=tw;
                    node.x=tx;
                    node.y=ty;
                    node.s=q.front().s+1;
                    q.push(node);
                }
                if(f!=0) break;
                q.pop();
            }
            if(f==0) printf("Trapped!
    ");
            else printf("Escaped in %d minute(s).
    ",f);
        }
        return 0;
    } 
  • 相关阅读:
    64.Find the Duplicate Number(发现重复数字)
    63.Perfect Squares(完美平方数)
    62.Longest Valid Parentheses(最长的有效括号)
    61.Merge k Sorted Lists(合并k个排序链表)
    60.Median of Two Sorted Arrays(两个排序数组的中位数)
    59.Target Sum(目标和)
    58.Partition Equal Subset Sum(判断一个数组是否可以分成和相等的两个数组)
    57.Queue Reconstruction by Height(按身高重建对列)
    56.Decode String(解码字符串)
    55.Top K Frequent Elements(出现次数最多的k个元素)
  • 原文地址:https://www.cnblogs.com/yzm10/p/7240302.html
Copyright © 2011-2022 走看看