zoukankan      html  css  js  c++  java
  • BFS POJ2251 Dungeon Master

    B - Dungeon Master
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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!

    BFS水题,套模板按6个方向拓展就行了。

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <algorithm>
      4 #include <queue>
      5 #include <string.h>
      6 using namespace std;
      7 int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
      8 char s[35][35][35];
      9 int maps[35][35][35];
     10 int x1,y1,z1;
     11 int x2,y2,z2;
     12 int L,R,C;
     13 struct data
     14 {
     15     int x;
     16     int y;
     17     int z;
     18     int step;
     19 };
     20 int check(int x,int y,int z)
     21 {
     22     if(x<0 || y<0 || z<0 || x>=L || y>=R || z>=C)
     23         return 1;
     24     else if(s[x][y][z] == '#')
     25         return 1;
     26     else if(maps[x][y][z])
     27         return 1;
     28     return 0;
     29 }
     30 int bfs()
     31 {
     32     queue<data> q;
     33     data now,nex;
     34     now.x=x1;
     35     now.y=y1;
     36     now.z=z1;
     37     now.step=0;
     38     maps[x1][y1][z1]=1;
     39     q.push(now);
     40     while(!q.empty())
     41     {
     42         now=q.front();
     43         q.pop();
     44         if(now.x==x2&&now.y==y2&&now.z==z2)
     45             {
     46                 return now.step;
     47             }
     48         for(int i=0;i<6;i++)
     49         {  nex=now;
     50             nex.x=now.x+to[i][0];
     51             nex.y=now.y+to[i][1];
     52             nex.z=now.z+to[i][2];
     53             if(check(nex.x,nex.y,nex.z))
     54             {
     55                 continue;
     56             }
     57             maps[nex.x][nex.y][nex.z]=1;
     58             nex.step=now.step+1;
     59            q.push(nex);
     60         }
     61     }
     62     return 0;
     63 }
     64 int main()
     65 {
     66     while(scanf("%d%d%d",&L,&R,&C),L+R+C)
     67     {
     68         cin>>R>>C;
     69         int i,j,k;
     70         for(i=0;i<L;i++) //
     71         {
     72             for(j=0;j<R;j++)
     73             {
     74                 scanf("%s",s[i][j]);
     75                 for(k=0;k<C;k++)
     76                 {
     77                     if(s[i][j][k]=='S')
     78                     {
     79                         x1=i;
     80                         y1=j;
     81                         z1=k;
     82                     }
     83                    else if(s[i][j][k]=='E')
     84                     {
     85                         x2=i;
     86                         y2=j;
     87                         z2=k;
     88                     }
     89                 }
     90             }
     91         }
     92         memset(maps,0,sizeof(maps));
     93         int ans;
     94         ans=bfs();
     95         if(ans)
     96         {printf("Escaped in %d minute(s).
    ",ans);}
     97         else
     98         {
     99             printf("Trapped!
    ");
    100         }
    101     }
    102     return 0;
    103 }
  • 相关阅读:
    Codeforces 449D:Jzzhu and Numbers
    51nod 1040:最大公约数之和
    51nod 1179:最大的最大公约数
    51nod 1406:与查询
    51nod 1354:选数字
    51nod 1616:最小集合
    Codeforces:Colored Balls
    素性测试
    秒转换成年月日时分秒 和复制文本到剪贴板
    vue项目中获取cdn域名插件
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/8384491.html
Copyright © 2011-2022 走看看