zoukankan      html  css  js  c++  java
  • POJ 2251

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23092   Accepted: 8985

    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 即可。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 using namespace std;
     5 struct pr{
     6     int x,y,z,t;
     7  };
     8  const int x[10]={0,0,0,1,-1,0,0},y[10]={0,1,-1,0,0,0,0},z[10]={0,0,0,0,0,1,-1};
     9  int a,b,c,sx,sy,sz;
    10  char map[111][111][111];
    11  int flag[111][111][111];
    12  queue<pr> s;
    13  pr q,temp;
    14  void bfs()
    15  {
    16     int i,j,k;
    17     while(!s.empty())
    18     s.pop();
    19     memset(flag,0,sizeof(flag));
    20     flag[sx][sy][sz]=1;
    21     q.x=sx;q.y=sy; q.z=sz; q.t=0;
    22     s.push(q);
    23     while(!s.empty())
    24     {
    25         q=s.front();
    26         s.pop(); 
    27         for(i=1;i<=6;i++)
    28             if(q.x+x[i]>0&&
    29                 q.x+x[i]<=a&&
    30                 q.y+y[i]>0&&
    31                 q.y+y[i]<=b&&
    32                 q.z+z[i]<=c&&
    33                 q.z+z[i]>0)
    34                 {
    35                 if(map[q.x+x[i]][q.y+y[i]][q.z+z[i]]!='#')
    36                     if(!flag[q.x+x[i]][q.y+y[i]][q.z+z[i]])
    37                     {
    38                         flag[q.x+x[i]][q.y+y[i]][q.z+z[i]]=1;
    39                         temp.x=q.x+x[i];
    40                         temp.y=q.y+y[i];
    41                         temp.z=q.z+z[i];
    42                         temp.t=q.t+1;
    43                         if(map[q.x+x[i]][q.y+y[i]][q.z+z[i]]=='E')
    44                         {
    45                             cout<<"Escaped in "<<temp.t<<" minute(s)."<<endl;
    46                             return;
    47                         }
    48                         s.push(temp);
    49                     }
    50                 }
    51     }
    52     cout<<"Trapped!"<<endl;
    53  }
    54 int main()
    55 {
    56     int i,j,k;
    57     while(cin>>a>>b>>c,a+b+c)
    58     {
    59         for(i=1;i<=a;i++)
    60             for(j=1;j<=b;j++)
    61                 for(k=1;k<=c;k++)
    62             {
    63                 cin>>map[i][j][k];
    64                 if(map[i][j][k]=='S')
    65                 {
    66                     sx=i;
    67                     sy=j;
    68                     sz=k;
    69                 }
    70             }
    71         bfs();
    72     }
    73 }
     
     
    我自倾杯,君且随意
  • 相关阅读:
    ubuntu开启SSH服务
    Ubuntu修改虚拟内存(即swap空间)
    【转】Ubuntu 13.10中MyEclipse 10.6+下载+安装+破解
    【转】 ubuntu下安装mysql
    【转】 Ubuntu 11.04 下安装配置 JDK 7
    Linux非root用户安装jdk和tomcat
    algorithm之改变序列算法--待解决
    时间日期设置--ctime头文件
    C中的一些函数
    algorithm之不变序列操作
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5155796.html
Copyright © 2011-2022 走看看