zoukankan      html  css  js  c++  java
  • poj 2251 Dungeon Master(bfs)

    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

     
    题意:一个3D的迷宫,问能否逃出去
    直接bfs。。。
     
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 #include<cmath>
      6 #include<stdlib.h>
      7 using namespace std;
      8 int k,n,m;
      9 char map[36][36][36];
     10 int vis[36][36][36];
     11 struct Node
     12 {
     13     int floor;
     14     int x,y;
     15     int t;
     16 }st,ed;
     17 int dirx[]={0,0,-1,1};
     18 int diry[]={-1,1,0,0};
     19 void bfs(Node s)
     20 {
     21     queue<Node>q;
     22     q.push(s);
     23     vis[s.floor][s.x][s.y]=1;
     24     Node t1,t2;
     25     while(!q.empty())
     26     {
     27         t1=q.front();
     28         q.pop();
     29         if(t1.floor==ed.floor && t1.x==ed.x && t1.y==ed.y)
     30         {
     31             printf("Escaped in %d minute(s).
    ",t1.t);
     32             return;    
     33         }
     34         
     35         for(int i=0;i<4;i++)
     36         {
     37             t2.floor=t1.floor;
     38             t2.x=t1.x+dirx[i];
     39             t2.y=t1.y+diry[i];
     40             if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
     41             {
     42                 vis[t2.floor][t2.x][t2.y]=1;
     43                 t2.t=t1.t+1;
     44                 q.push(t2);
     45             }
     46         }
     47         t2=t1;
     48         t2.floor=t1.floor+1;
     49         if(t2.floor<0 || t2.floor>=k) continue;
     50         if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
     51         {
     52                 vis[t2.floor][t2.x][t2.y]=1;
     53                 t2.t=t1.t+1;
     54                 q.push(t2);
     55         }
     56         
     57         t2=t1;
     58         t2.floor=t1.floor-1;
     59         if(t2.floor<0 || t2.floor>=k) continue;
     60         if(t2.x>=0 && t2.x<n && t2.y>=0 && t2.y<m && !vis[t2.floor][t2.x][t2.y] && map[t2.floor][t2.x][t2.y]!='#')
     61         {
     62                 vis[t2.floor][t2.x][t2.y]=1;
     63                 t2.t=t1.t+1;
     64                 q.push(t2);
     65         }
     66     }
     67     printf("Trapped!
    ");
     68 }
     69 int main()
     70 {
     71     while(scanf("%d%d%d",&k,&n,&m)==3 && n+m+k!=0)
     72     {
     73         for(int i=0;i<k;i++)
     74         {
     75             for(int j=0;j<n;j++)
     76             {
     77                 scanf("%s",map[i][j]);
     78                 for(int l=0;l<m;l++)
     79                 {
     80                     if(map[i][j][l]=='S')
     81                     {
     82                         st.floor=i;
     83                         st.x=j;
     84                         st.y=l;
     85                         st.t=0;
     86                     }
     87                     if(map[i][j][l]=='E')
     88                     {
     89                         ed.floor=i;
     90                         ed.x=j;
     91                         ed.y=l;
     92                     }
     93                 }
     94             }
     95         }
     96         memset(vis,0,sizeof(vis));
     97         bfs(st);
     98     }
     99     return 0;
    100 }
    View Code
  • 相关阅读:
    雷锋依然在人间 工厂方法模式
    为别人做嫁衣 代理模式
    穿什么有这么重要? 装饰模式
    437. Path Sum III
    434. Number of Segments in a String
    447. Add Strings
    414. Third Maximum Number
    412. Fizz Buzz
    404. Sum of Left Leaves
    405. Convert a Number to Hexadecimal
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4732898.html
Copyright © 2011-2022 走看看