POJ2251:Dungeon Master

来源:互联网 发布:机器人编程工资待遇 编辑:程序博客网 时间:2024/04/25 07:53

点击打开题目链接


Dungeon Master

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13904 Accepted: 5411

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997


====================================题目大意======================================


尽快逃出规格为 L X R X C 的3D地牢。


====================================算法分析======================================


简单BFS,在普通2D搜索题的基础上稍加修改即可。


=======================================代码=======================================




#include<queue>#include<cstdio>#include<string>using namespace std;#define DIS(P)      Dis[P.lv][P.row][P.col]                                           //坐标对应的Dis值#define MAP(P)      Map[P.lv][P.row][P.col]                                           //坐标对应的Map值#define SAME(A,B)   ((A.lv==B.lv)&&(A.row==B.row)&&(A.col==B.col))                    //相同坐标#define LEGCORD(P)  ((0<=P.lv&&P.lv<L)&&(0<=P.row&&P.row<R)&&(0<=P.col&&P.col<C))     //合法坐标const int Dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int L,R,C,Dis[35][35][35];char Map[35][35][35];struct COORD { int lv,row,col; }S,E;COORD MoveCoord(COORD Cur,int N)                                                      //坐标Cur朝方向N移动一次后得到的坐标  {COORD tmp;tmp.lv =Cur.lv +Dir[N][0];tmp.row=Cur.row+Dir[N][1];tmp.col=Cur.col+Dir[N][2];return tmp;}void BFS(){memset(Dis,-1,sizeof(Dis));DIS(S)=0;queue<COORD> q;for(q.push(S);!q.empty();q.pop()){COORD cur=q.front();  if(SAME(cur,E)) { printf("Escaped in %d minute(s).\n",DIS(E));  return;}for(int n=0;n<6;++n){COORD tmp=MoveCoord(cur,n);if(LEGCORD(tmp)&&MAP(tmp)!='#'&&DIS(tmp)==-1){DIS(tmp)=DIS(cur)+1;q.push(tmp);}}}printf("Trapped!\n");}void ReaData(){for(int i=0;i<L;++i){for(int j=0;j<R;++j){scanf("%s",Map[i][j]);for(int k=0;k<C;++k){if(Map[i][j][k]=='S') { S.lv=i;  S.row=j;  S.col=k; }if(Map[i][j][k]=='E') { E.lv=i;  E.row=j;  E.col=k; }} }}}int main(){while(scanf("%d%d%d",&L,&R,&C)==3&&(L||R||C)){ReaData();BFS();}return 0;}

原创粉丝点击