Dungeon Master ZOJ 1940【优先队列+广搜】
来源:互联网 发布:教师网络培训服务平台 编辑:程序博客网 时间:2024/06/05 08:19
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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!
//一次AC,感觉太爽了!!
#include<cstdio>#include<cstring>#include<queue>using namespace std;char map[40][40][40];int x1,y1,z1,x2,y2,z2;int dx[]={0,1,0,-1,0,0};int dy[]={1,0,-1,0,0,0};int dz[]={0,0,0,0,1,-1};int n,row,column;struct Dungeon//地牢 {int x,y,z;int time;friend bool operator < (Dungeon a,Dungeon b){return a.time>b.time;}};bool judge(int xt,int yt,int zt){if(xt>row||xt<1||yt>column||yt<1||zt>n||zt<1) //越界 return 0;if(map[zt][xt][yt]=='#') return 0;return 1;}int BFS(){priority_queue<Dungeon>q;Dungeon pos,next;pos.x=x1;pos.y=y1;pos.z=z1;pos.time=0;map[z1][x1][y1]='#';q.push(pos);while(!q.empty()){pos=q.top();q.pop();for(int i=0;i<6;++i){next.x=pos.x+dx[i];next.y=pos.y+dy[i];next.z=pos.z+dz[i];next.time=pos.time+1;if(judge(next.x,next.y,next.z)){if(next.x==x2&&next.y==y2&&next.z==z2){return next.time;}map[next.z][next.x][next.y]='#';q.push(next);}}}return -1;}int main(){while(~scanf("%d%d%d",&n,&row,&column),n+row+column){getchar();for(int i=1;i<=n;++i){for(int j=1;j<=row;++j){for(int k=1;k<=column;++k){scanf("%c",map[i][j]+k);if(map[i][j][k]=='S'){z1=i,x1=j,y1=k;}else if(map[i][j][k]=='E'){z2=i,x2=j,y2=k;}}getchar();}getchar();}int res;res=BFS();if(res==-1) printf("Trapped!\n");else printf("Escaped in %d minute(s).\n",res);}return 0;}
1 0
- Dungeon Master ZOJ 1940【优先队列+广搜】
- POJ2251 Dungeon Master(三维广搜BFS+优先队列)
- ZOJ 1940 Dungeon Master (三维广搜)
- 【搜索】— 广搜队列 noi openjudge 2.5 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- zoj - 1940 - Dungeon Master
- zoj 1940 Dungeon Master
- zoj 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- ZOJ 1940 Dungeon Master
- zoj 1940 Dungeon Master
- POJ2251--Dungeon Master--广搜
- HDU Dungeon Master广搜
- Dungeon Master (广搜)
- Dungeon Master(广搜)
- C/C++打开文件函数fopen和fopen_s用法的比较
- struts2学习笔记(二)
- 读写文件
- MyBatis insert和select例子
- Java 算法编程 N阶乘末尾0的个数问题
- Dungeon Master ZOJ 1940【优先队列+广搜】
- HDU 5019 Revenge of GCD(简单枚举)
- php5中虚函数的实现
- grep, egrep
- Android Design Support Library使用示例(一)
- Android 登录注册功能
- 2015 multiply 6 1011
- 基于邻接矩阵的广度优先搜索遍历 (BFS)
- 最少步数 bfs样板