poj2251 三维bfs求最短距离
来源:互联网 发布:win7优化精简教程 编辑:程序博客网 时间:2024/05/19 16:03
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22466 Accepted: 8769
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!
题意:三维的地图,求起点到终点的最短距离
分析:三维bfs,多判断两个方向上下就行 。
#include<bitset>#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}struct st{ int x,y,z;};const int N=100100;char a[33][33][33]; //层数 x yint dis[33][33][33];int l,n,m;int dx[]={-1,0,1,0},dy[]={0,-1,0,1};int bfs(st start,st end){ queue<st> q; q.push(start); dis[start.x][start.y][start.z]=0; while(!q.empty()) { st now = q.front(); q.pop(); if(now.x==end.x && now.y==end.y && now.z==end.z) return dis[end.x][end.y][end.z]; for(int i=0;i<4;i++) { int nk=now.x; int nx=now.y+dx[i]; int ny=now.z+dy[i]; if(nk>=0 && nx>=0 && ny>=0 && nk<l && nx<n && ny<m && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf) { dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1; q.push(st{nk,nx,ny}); } } int nk=now.x+1; int nx=now.y; int ny=now.z; if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf) { dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1; q.push(st{nk,nx,ny}); } nk=now.x-1; nx=now.y; ny=now.z; if(nk>=0 && nk<l && a[nk][nx][ny]=='.' && dis[nk][nx][ny]==inf) { dis[nk][nx][ny]=dis[now.x][now.y][now.z]+1; q.push(st{nk,nx,ny}); } } return dis[end.x][end.y][end.z];}int main(){ while(~scanf("%d%d%d",&l,&n,&m) && l) { for(int k=0;k<l;k++) { for(int i=0;i<n;i++) scanf("%s",a[k][i]); } mem(dis,inf); st start,end; for(int k=0;k<l;k++) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(a[k][i][j]=='S') { start=st{k,i,j}; } else if(a[k][i][j]=='E') { end=st{k,i,j}; a[k][i][j]='.';//注意要把end位置设置为'.',要不然会找不到,这里错了好几次 } } } } int ans=bfs(start,end); if(ans==inf) puts("Trapped!"); else printf("Escaped in %d minute(s).\n",ans); } return 0;}
0 0
- poj2251 三维bfs求最短距离
- POJ2251(BFS求最短路)
- POJ2251 三维的bfs
- poj2251 三维bfs
- POJ2251-简单三维BFS
- POJ2251---Dungeon Master---BFS最短路之三维迷宫
- POJ2251 Dungeon Master 三维BFS
- 三维BFS poj2251 Dungeon Master
- POJ2251-Dungeon Master (三维BFS)
- 求三维凸包重心到表面最短距离
- hdu1026(bfs求起点到终点最短距离)
- bfs两个起点求两者共同的最短距离
- POJ2251——BFS三维迷宫
- poj2251 Dungeon Master(三维bfs)
- HDU4273(求三维凸包重心到表面的最短距离)
- UVA 152-Tree's a Crowd(暴力求解三维坐标求最短距离)
- POJ2225 Asteroids! (三维的BFS求最短路)
- POJ2251,Dungeon Master,三维bfs水,切起来很爽
- MySQL 常用维护命令
- 如何将android studio项目转换成eclipse
- 2049: [Sdoi2008]Cave 洞穴勘测
- 关于导入GPUimage到工程以及简单使用
- android判断当前系统的语言
- poj2251 三维bfs求最短距离
- 2015河南科技学院高校CSDN俱乐部--excel讲解
- MySQL 常用管理工具
- Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0:
- python实践——批量统计mongodb数据库的集合大小
- 路由器与交换机
- Linux Memory Mapping
- 安装11G数据库脚本
- iOS8以上的版本,使用UIAlertController 替代 UIAlert 弹窗