zoukankan      html  css  js  c++  java
  • POJ 2251 Dungeon Master

    Dungeon Master
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13187   Accepted: 5113

    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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 
     6 using namespace std;
     7 
     8 char map[40][40][40];
     9 int vis[40][40][40];
    10 int L,R,C;
    11 
    12 struct node{
    13     int z,x,y;
    14     int c;
    15 };
    16 
    17 int dir[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
    18 
    19 int BFS(int si,int sj,int sk){
    20     queue<node> q;
    21     while(!q.empty())
    22         q.pop();
    23     memset(vis,0,sizeof(vis));
    24     node cur,next;
    25     cur.z=si,cur.x=sj,cur.y=sk,cur.c=0;
    26     vis[si][sj][sk]=1;
    27     q.push(cur);
    28     while(!q.empty()){
    29         cur=q.front();
    30         q.pop();
    31         for(int i=0;i<6;i++){
    32             next.z=cur.z+dir[i][0];
    33             next.x=cur.x+dir[i][1];
    34             next.y=cur.y+dir[i][2];
    35             next.c=cur.c+1;
    36             if(next.z<1 || next.z>L || next.x<1 || next.x>R || next.y<1 || next.y>C || map[next.z][next.x][next.y]=='#')
    37                 continue;
    38             if(map[next.z][next.x][next.y]=='E')
    39                 return next.c;
    40             if(!vis[next.z][next.x][next.y]){
    41                 vis[next.z][next.x][next.y]=1;
    42                 q.push(next);
    43             }
    44         }
    45     }
    46     return 0;
    47 }
    48 
    49 int main(){
    50 
    51     //freopen("input.txt","r",stdin);
    52 
    53     int si,sj,sk;
    54     while(cin>>L>>R>>C){
    55         if(L==0 && R==0 && C==0)
    56             break;
    57         for(int i=1;i<=L;i++)
    58             for(int j=1;j<=R;j++)
    59                 for(int k=1;k<=C;k++){
    60                     cin>>map[i][j][k];
    61                     if(map[i][j][k]=='S'){
    62                         si=i;sj=j;sk=k;
    63                     }
    64                 }
    65         int ans=BFS(si,sj,sk);
    66         if(ans==0)
    67             printf("Trapped!\n");
    68         else
    69             printf("Escaped in %d minute(s).\n",ans);
    70 
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 735 行星碰撞(栈)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 LeetCode 887 鸡蛋掉落(动态规划,谷歌面试题,蓝桥杯真题)
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Java实现 蓝桥杯算法提高 求最大值
    Python eval() 函数
    Python repr() 函数
  • 原文地址:https://www.cnblogs.com/jackge/p/3023841.html
Copyright © 2011-2022 走看看