码迷,mamicode.com
首页 > 其他好文 > 详细

Dungeon Master

时间:2015-08-06 01:56:38      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 9
Problem 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
PKU
题解:bfs做的,poj老是ml,内存过大,无奈了,错了无数次,但是hdoj的webdiy竟然过了。。。
代码:
 1 #include<stdio.h>
 2 #include<queue>
 3 using namespace std;
 4 //const int INF=0xfffffff;
 5 struct Node{
 6     int nx,ny,nz,step;
 7     friend bool operator < (Node a,Node b){
 8         return a.step>b.step;
 9     }
10 };
11 Node a,b;
12 char map[40][40][40];
13 int vis[40][40][40];
14 int disx[6]={0, 0, 1,-1,0, 0};
15 int disy[6]={1,-1, 0, 0,0, 0};
16 int disz[6]={0, 0, 0, 0,1,-1};
17 int sx,sy,sz,ex,ey,ez,minstep,flot,L,R,C;
18 priority_queue<Node>dl;
19 bool judge(Node s){
20     if(s.nx<0||s.nx>=R)return false;
21     if(s.ny<0||s.ny>=C)return false;
22     if(s.nz<0||s.nz>=L)return false;
23     if(vis[s.nz][s.nx][s.ny])return false;//写错了,找了半天。。。 
24     return true;
25 }
26 void bfs(){
27     a.nx=sx;a.ny=sy;a.nz=sz;a.step=0;
28     dl.push(a);
29     while(!dl.empty()){
30         a=dl.top();
31         dl.pop();
32         vis[a.nz][a.nx][a.ny]=1;
33         if(a.nx==ex&&a.ny==ey&&a.nz==ez){
34             minstep=a.step;
35             flot=1;
36             //if(a.step<minstep)minstep=a.step;
37             return;
38         }
39         for(int i=0;i<6;i++){
40             b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.nz=a.nz+disz[i];b.step=a.step+1;
41             if(!judge(b))continue;
42             if(map[b.nz][b.nx][b.ny]==.||map[b.nz][b.nx][b.ny]==E)dl.push(b);
43         }
44     }
45 }
46 int main(){int x,y,z;
47     while(~scanf("%d%d%d",&L,&R,&C),L||R||C){flot=0;//minstep=INF;
48     while(!dl.empty())dl.pop(); 
49         for(z=0;z<L;z++)for(x=0;x<R;x++)scanf("%s",map[z][x]);
50         for(z=0;z<L;z++)for(x=0;x<R;x++){//printf("%s\n",map[z][x]);
51         for(y=0;y<C;y++){
52             if(map[z][x][y]==S)sx=x,sy=y,sz=z;
53             if(map[z][x][y]==E)ex=x,ey=y,ez=z;
54         }    
55         }
56         //printf("%d %d %d %d %d %d\n",sx,sy,sz,ex,ey,ez);
57         bfs();
58         if(flot){
59             printf("Escaped in %d minute(s).\n",minstep);
60         }
61         else puts("Trapped!");
62     }
63     return 0;
64 }

 

Dungeon Master

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4706227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!