算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
来源:互联网 发布:网强网络管理系统 编辑:程序博客网 时间:2024/04/30 02:10
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 27195 Accepted: 10668
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!
Source
Ulm Local 1997
典型的最短路径问题,将二维的BFS扩展到三维即可。
在此给出AC代码:
//三维bfs #include<stdio.h>#include<iostream>#include<queue>using namespace std;const int maxn=31;int map[maxn][maxn][maxn];int vis[maxn][maxn][maxn];int sx,sy,sz,ex,ey,ez;int c,a,b;int dirx[6]= {-1,1,0,0,0,0};int diry[6]= {0,0,-1,1,0,0};int dirz[6]= {0,0,0,0,-1,1};structnode{int x,y,z,step;};int BFS(){queue<node>q;node p1;p1.x=sx;p1.y=sy;p1.z=sz;vis[sx][sy][sz]=1;p1.step=0;q.push(p1);//printf("%d %d %d\n",p1.x,p1.y,p1.z);while(!q.empty()){node f=q.front(),temp;q.pop();if(f.x==ex&&f.y==ey&&f.z==ez)return f.step;for(int i=0; i<6; i++){temp.x=f.x+dirx[i];temp.y=f.y+diry[i];temp.z=f.z+dirz[i];temp.step=f.step+1;if(temp.x<a&&temp.x>=0&&temp.y<b&&temp.y>=0&&temp.z<c&&temp.z>=0&& !vis[temp.x][temp.y][temp.z]&&(map[temp.x][temp.y][temp.z]==1)){q.push(temp);//printf("%d %d %d\n",temp.x,temp.y,temp.z);vis[temp.x][temp.y][temp.z]=1;}}}return -1;}int main(){while(~scanf("%d%d%d",&c,&a,&b),a,b,c){char temp;for(int i=0; i<c; i++)//平面坐标for(int j=0; j<a; j++)//j是平面xfor(int k=0; k<b; k++)//k是平面y{vis[j][k][i]=0;cin>>temp;if(temp=='.')map[j][k][i]=1;else if(temp=='#')map[j][k][i]=0;else if(temp=='S'){sx=j;sy=k;sz=i;map[j][k][i]=1;}else if(temp=='E'){ex=j;ey=k;ez=i;map[j][k][i]=1;}}int res=BFS();if(res==-1)printf("Trapped!\n");elseprintf("Escaped in %d minute(s).\n",res);}return 0;}
0 0
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
- POJ 2251 Dungeon Master 三维最短路径
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master (三维迷宫 BFS)
- POJ 2251 Dungeon Master(三维BFS)
- poj 2251 Dungeon Master 三维bfs
- poj 2251 Dungeon Master(三维BFS)(中等)
- poj 2251 Dungeon Master(BFS三维)
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master(三维bfs)
- <三维BFS搜索> POJ 2251 Dungeon Master
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master 三维BFS
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251(WA)Dungeon Master(三维BFS)
- 自建app服务器架构特点
- Dagger2 最清晰的使用教程
- parasoft Jtest 使用教程:修改规则与运行简单的用户自定义测试配置
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
- cocos2dx项目导入到安卓中
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径
- 算法-排序-快速排序
- Python学习-02
- MUI 下拉刷新
- 怎样编写高质量的Java代码
- 欢迎使用CSDN-markdown编辑器
- 场景化学习 git
- Java调用weka的各种聚类算法
- jsonp跨域