POJ-2251 Dungeon Master

来源:互联网 发布:修改定位软件 编辑:程序博客网 时间:2024/06/05 07:29

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

Sample Output

Escaped in 11 minute(s). 
Trapped! 


这道题呢其实就是一个迷宫问题,只不过从二维变成了三维而已
首先呢这道题是用宽搜做的,可以从其数据范围看出
我本人一开始用深搜做,然后一直超时,看了数据范围以后才用的宽搜(其实最短路时是最好用宽搜的)
#include<iostream>#include<queue>using namespace std;queue<int>p1;//宽搜的队列queue<int>p2;queue<int>p3;int n,m,x,end1,end2,end3,a[31][31][31]={0},st1,st2,st3;int b[3][6]= {{1,-1,0,0,0,0},{0,0,1,-1,0,0},{0,0,0,0,1,-1}};//6个方向char s[31][31][31];int search(){for(int i=1;i<=30;i++){//初始化   for(int j=1;j<=30;j++){     for(int k=1;k<=30;k++){             a[i][j][k]=11111111;      }   } }a[st1][st2][st3]=0;//起点是0p1.push(st1);p2.push(st2);p3.push(st3);while(p1.size()&&p2.size()&&p3.size()){//当队列不为空时就一直执行int s1=p1.front();p1.pop();int s2=p2.front();p2.pop();int s3=p3.front();p3.pop();if(s1==end1&&s2==end2&&s3==end3)break;for(int i=0;i<=5;i++){int t1=s1+b[0][i],t2=s2+b[1][i],t3=s3+b[2][i];if(a[s1][s2][s3]+1<a[t1][t2][t3]&&t1<=n&&t1>=1&&t2<=m&&t2>=1&&t3<=x&&t3>=1&&s[t1][t2][t3]!='#'){a[t1][t2][t3]=a[s1][s2][s3]+1;p1.push(t1);p2.push(t2);p3.push(t3);}}}return a[end1][end2][end3];}int main() {while(cin>>n>>m>>x&&n&&m&&x){for(int i=1; i<=n; i++) {for(int j=1; j<=m; j++) {for(int k=1; k<=x; k++) {cin>>s[i][j][k];if(s[i][j][k]=='E') {//记录终点end1=i;end2=j;end3=k;}if(s[i][j][k]=='S') {//记录开始点st1=i;st2=j;st3=k;}}}}inth=search();if(h!=11111111)cout<<"Escaped in "<<h<<" minute(s).";else cout<<"Trapped!";cout<<endl;}}

原创粉丝点击