poj 2251(Dungeon Master)
来源:互联网 发布:java打印三角形思路 编辑:程序博客网 时间:2024/06/05 11:31
Dungeon Master(BFS)
Dungeon Master
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 26643
Accepted: 10393
Memory Limit: 65536KTotal Submissions: 26643
Accepted: 10393
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!
题意:三维空间,问从S到E多少步题解:模拟三维,bfs()六个方向
#include<cstdio>#include<cstring>#include<queue>using namespace std;int L,R,C;char map[33][33][33];int vis[33][33][33];struct node{int x,y,z,step;}a,b;int xi,yi,xe,ye,zi,ze;int dx[6]={0,0,1,-1,0,0};int dy[6]={1,-1,0,0,0,0};int dz[6]={0,0,0,0,1,-1};int check(int aa,int bb,int cc){if (aa>=0&&aa<L &&bb>=0&&bb<R &&cc>=0&&cc<C&&!vis[aa][bb][cc]&&map[aa][bb][cc]!='#')return 1;return 0;}int bfs(){int i;a.x=xi,a.y=yi,a.z=zi,a.step=0;vis[a.x][a.y][a.z]=1;queue<node> q;q.push(a);while (!q.empty()){a=q.front();q.pop();if (a.x==xe && a.y==ye &&a.z==ze){return a.step;}for (i=0;i<6;i++){b=a;b.x+=dx[i],b.y +=dy[i],b.z +=dz[i];if (!check(b.x,b.y,b.z)) continue;vis[b.x][b.y][b.z] = 1;//忘标记 b.step=a.step+1;//不能直接b.step++ q.push(b);}}return 0;}int main(){int i,j,k;while (scanf ("%d %d %d",&L,&R,&C)!=EOF){if (L==0 && R==0 && C==0) break;getchar();for (i=0;i<L;i++,getchar()){for (j=0;j<R;j++,getchar()){scanf ("%s",&map[i][j]);for (k=0;k<C;k++){if (map[i][j][k]=='S')xi=i,yi=j,zi=k;else if (map[i][j][k]=='E')xe=i,ye=j,ze=k;//x,y,z的坐标弄错 }}}memset (vis,0,sizeof(vis));int ans = 0;ans = bfs();if (ans)printf ("Escaped in %d minute(s).\n",ans);elseprintf ("Trapped!\n");}return 0;}
0 0
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- Poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- POJ 2251Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 - Dungeon Master
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- poj 2251 Dungeon Master
- 超简单的时间分钟秒钟倒计时
- IOS中获取各种文件的目录路径的方法
- JCBC-事务
- Java编程题目-5:回文数
- javascript创建对象
- poj 2251(Dungeon Master)
- MacOS系统熟悉
- (数据结构)oj重建二叉树(用权值作为新二叉树的下标)
- linux兴趣小组暑假第五次讲座总结
- 一条线上按距离找点的qt绘图
- java中关键字volatile的作用
- 学习排序
- AlertDialog的七种形式
- 关于 PHP 性能优化