poj2251 三维bfs求最短距离
2015-12-16 19:57
330 查看
Dungeon Master
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?
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
Sample Output
题意:三维的地图,求起点到终点的最短距离
分析:三维bfs,多判断两个方向上下就行 。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22466 | Accepted: 8769 |
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #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 y int 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; }
相关文章推荐
- Linux Kernel中的数据结构---- 其它
- leetcode -- Merge Intervals -- 典型trick
- MySQL 日志
- android studio项目如何导入到eclipse中
- android开发中scrollview添加自定义view的滑动显示问题
- Linux Memory Mapping
- iOS8以上的版本,使用UIAlertController 替代 UIAlert 弹窗
- tomcat无法启动
- MySQL 字符集
- 杭电3308 LCIS(线段树区间合并)
- 大端模式与小端模式、网络字节顺序与主机字节顺序 (经典)
- 安装11G数据库脚本
- python实践——批量统计mongodb数据库的集合大小
- Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0:
- Android读书笔记之Fragment入门
- MySQL 常用管理工具
- NodeJs oracledb无法完成删除数据项操作的处理方法
- python实践——批量统计mongodb数据库的集合大小
- android判断当前系统的语言
- 2049: [Sdoi2008]Cave 洞穴勘测