• Dungeon Master


    Dungeon Master

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 11   Accepted Submission(s) : 9
    Problem 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
    PKU
    题解:bfs做的,poj老是ml,内存过大,无奈了,错了无数次,但是hdoj的webdiy竟然过了。。。
    代码:
     1 #include<stdio.h>
     2 #include<queue>
     3 using namespace std;
     4 //const int INF=0xfffffff;
     5 struct Node{
     6     int nx,ny,nz,step;
     7     friend bool operator < (Node a,Node b){
     8         return a.step>b.step;
     9     }
    10 };
    11 Node a,b;
    12 char map[40][40][40];
    13 int vis[40][40][40];
    14 int disx[6]={0, 0, 1,-1,0, 0};
    15 int disy[6]={1,-1, 0, 0,0, 0};
    16 int disz[6]={0, 0, 0, 0,1,-1};
    17 int sx,sy,sz,ex,ey,ez,minstep,flot,L,R,C;
    18 priority_queue<Node>dl;
    19 bool judge(Node s){
    20     if(s.nx<0||s.nx>=R)return false;
    21     if(s.ny<0||s.ny>=C)return false;
    22     if(s.nz<0||s.nz>=L)return false;
    23     if(vis[s.nz][s.nx][s.ny])return false;//写错了,找了半天。。。 
    24     return true;
    25 }
    26 void bfs(){
    27     a.nx=sx;a.ny=sy;a.nz=sz;a.step=0;
    28     dl.push(a);
    29     while(!dl.empty()){
    30         a=dl.top();
    31         dl.pop();
    32         vis[a.nz][a.nx][a.ny]=1;
    33         if(a.nx==ex&&a.ny==ey&&a.nz==ez){
    34             minstep=a.step;
    35             flot=1;
    36             //if(a.step<minstep)minstep=a.step;
    37             return;
    38         }
    39         for(int i=0;i<6;i++){
    40             b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.nz=a.nz+disz[i];b.step=a.step+1;
    41             if(!judge(b))continue;
    42             if(map[b.nz][b.nx][b.ny]=='.'||map[b.nz][b.nx][b.ny]=='E')dl.push(b);
    43         }
    44     }
    45 }
    46 int main(){int x,y,z;
    47     while(~scanf("%d%d%d",&L,&R,&C),L||R||C){flot=0;//minstep=INF;
    48     while(!dl.empty())dl.pop(); 
    49         for(z=0;z<L;z++)for(x=0;x<R;x++)scanf("%s",map[z][x]);
    50         for(z=0;z<L;z++)for(x=0;x<R;x++){//printf("%s
    ",map[z][x]);
    51         for(y=0;y<C;y++){
    52             if(map[z][x][y]=='S')sx=x,sy=y,sz=z;
    53             if(map[z][x][y]=='E')ex=x,ey=y,ez=z;
    54         }    
    55         }
    56         //printf("%d %d %d %d %d %d
    ",sx,sy,sz,ex,ey,ez);
    57         bfs();
    58         if(flot){
    59             printf("Escaped in %d minute(s).
    ",minstep);
    60         }
    61         else puts("Trapped!");
    62     }
    63     return 0;
    64 }

     加了个优先队列,反而错了半天,原因是队列那个小于号要是大于号;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <set>
    #include <queue>
    using namespace std;
    int L, R, C;
    int ans;
    char mp[135][135][135];
    int disx[6] = {0,0,0,0,-1,1};
    int disy[6] = {1,-1,0,0,0,0};
    int disz[6] = {0,0,1,-1,0,0};
    int vis[135][135][135];
    /*
    void dfs(int z, int x, int y, int step){
        if(x < 0 || y < 0 || z < 0 || z >= L || x >= R || y >= C){
            return;
        }
        if(mp[z][x][y] == '#'){
            return;
        }
        if(mp[z][x][y] == 'E'){
            ans = min(ans, step);
            return;
        }
        for(int i = 0; i < 6; i++){
            int xx = x + disx[i];
            int yy = y + disy[i];
            int zz = z + disz[i];
            mp[z][x][y] = '#';
            dfs(zz, xx, yy, step + 1);
            mp[z][x][y] = '.';
        }
        
    }
    */
    struct Node{
        int x, y, z;
        int step;
        friend bool operator < (Node a, Node b){
            return a.step > b.step;
        }
    };
    void bfs(Node s){
        priority_queue<Node>q;
        while(!q.empty()){
            q.pop();
        }
        vis[s.z][s.x][s.y] = 1;
        q.push(s);
        Node e;
        while(!q.empty()){
            s = q.top();
            q.pop();
        //    vis[s.z][s.x][s.y] = 0;
            for(int i = 0; i < 6; i++){
                e.x = s.x + disx[i];
                e.y = s.y + disy[i];
                e.z = s.z + disz[i];
                e.step = s.step + 1;
                if(e.x < 0 || e.y < 0 || e.z < 0 || e.z >= L || e.x >= R || e.y >= C){
                    continue;
                }
                if(mp[e.z][e.x][e.y] == '#' || vis[e.z][e.x][e.y] == 1){
                    continue;
                }
                if(mp[e.z][e.x][e.y] == 'E'){
                    ans = min(ans, e.step);
                    return;
                }
                vis[e.z][e.x][e.y] = 1;
                q.push(e);
            }
        }
    }
    int main(){
        while(scanf("%d%d%d", &L, &R, &C), L|R|C){
            memset(vis, 0, sizeof(vis));
            Node s;
            for(int i = 0; i < L; i++){
                for(int j = 0; j < R; j++){
                    scanf("%s", mp[i][j]);
                    for(int k = 0; k < C; k++){
                        if(mp[i][j][k] == 'S'){
                            s.z = i;
                            s.x = j;
                            s.y = k;
                            s.step = 0;
                        }
                    }
                }
            }
            ans = 0x3f3f3f3f;
            //dfs(z, x, y, 0);
            bfs(s);
            if(ans == 0x3f3f3f3f){
                puts("Trapped!");
            }else{
                printf("Escaped in %d minute(s).
    ", ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon
    poj2241 The Tower of Babylon
    2015 Noip提高组 Day2
    2015 Noip提高组 Day1
    poj3252 Round Numbers
    hdu4734 F(x)
    hdu2089 不要62
    洛谷P1831 杠杆数
    洛谷P2113 看球泡妹子
    洛谷P3110 [USACO14DEC]驮运Piggy Back
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4706227.html
Copyright © 2020-2023  润新知