poj 2251 Dungeon Master(三维BFS)(中等)
2015-07-27 13:31
429 查看
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: 20598 | Accepted: 7971 |
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 <iostream> #include<cstring> #include<queue> #include<cstdio> using namespace std; typedef struct node { int x,y,z; int lenth; }node; node sta; int xx[]={1,-1,0,0,0,0}; int yy[]={0,0,0,0,-1,1}; int zz[]={0,0,-1,1,0,0}; bool vis[40][40][40]; char ma[40][40][40]; int l,r,c; int bfs() { queue<node> Q; int dx,dy,dz; memset(vis,false,sizeof(vis)); sta.lenth=0; Q.push(sta); vis[sta.x][sta.y][sta.z]=true; while(!Q.empty()) { node cur=Q.front(); Q.pop(); for(int i=0;i<6;i++) { node next; dx=cur.x+xx[i]; dy=cur.y+yy[i]; dz=cur.z+zz[i]; if(!vis[dx][dy][dz] && (ma[dx][dy][dz]=='.' || ma[dx][dy][dz]=='E') && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c) { vis[dx][dy][dz]=true; next.x=dx; next.y=dy; next.z=dz; next.lenth=cur.lenth+1; Q.push(next); if(ma[dx][dy][dz]=='E') return next.lenth; } } } return 0; } int main() { int i,j,k,key; while(scanf("%d%d%d\n",&l,&r,&c)) { if(l==0 && r==0 && c==0) break; for(i=0;i<l;i++,getchar()) for(j=0;j<r;j++,getchar()) for(k=0;k<c;k++) { scanf("%c",&ma[i][j][k]); if(ma[i][j][k]=='S') sta.x=i,sta.y=j,sta.z=k; } key=bfs(); if(key) printf("Escaped in %d minute(s).\n",key); else printf("Trapped!\n"); } return 0; }
//数组,AC #include <iostream> #include<cstring> #include<cstdio> using namespace std; struct q{ int x,y,z; }q[30000]; int length[30000]; int xx[]={1,-1,0,0,0,0}; int yy[]={0,0,0,0,-1,1}; int zz[]={0,0,-1,1,0,0}; bool dis[40][40][40]; char ma[40][40][40]; int l,r,c,sx,sy,sz; int bfs() { int rear,front,dx,dy,dz,i; memset(dis,false,sizeof(dis)); memset(length,0,sizeof(length)); q[0].x=sx,q[0].y=sy,q[0].z=sz; front=rear=0; while(front<=rear) { for(i=0;i<6;i++) { dx=q[front].x+xx[i]; dy=q[front].y+yy[i]; dz=q[front].z+zz[i]; if(!dis[dx][dy][dz] && (ma[dx][dy][dz]=='.' || ma[dx][dy][dz]=='E') && dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c) { dis[dx][dy][dz]=true; q[++rear].x=dx; q[rear].y=dy; q[rear].z=dz; length[rear]=length[front]+1; if(ma[dx][dy][dz]=='E') return length[rear]; } } front++; } return 0; } int main() { int i,j,k,key; while(scanf("%d%d%d\n",&l,&r,&c)) { if(l==0 && r==0 && c==0) break; for(i=0;i<l;i++,getchar()) for(j=0;j<r;j++,getchar()) for(k=0;k<c;k++) { scanf("%c",&ma[i][j][k]); if(ma[i][j][k]=='S') sx=i,sy=j,sz=k; } key=bfs(); if(key) printf("Escaped in %d minute(s).\n",key); else printf("Trapped!\n"); } return 0; }
相关文章推荐
- [单调队列] hdu 3415 Max Sum of Max-K-sub-sequence
- 【leetcode-16】3Sum Closest(java)
- 提高ios开发效率
- _CrtIsValidHeapPointer 跨dll接口参数数据传递问题
- 7.27计划
- javascript中的闭包
- Poj 1201 差分约束问题 详解
- hdu 3008 Warcraft
- HDU 5074 Hatsune Miku (动态规划)
- Spark Streaming 流计算优化记录(1)-背景介绍
- 2.朴素贝叶斯Naive Bayes
- mac shell终端编辑命令行快捷键——行首行尾
- 随机数
- c++获取系统d当前时间
- Linux文件的软链接和硬链接
- hdu 5312 Sequence
- Java取得指定年月的最大天数
- C++小品:井水不犯河水的thread_specific_ptr,C++11线程库中的本地存储
- Linux文件的软链接和硬链接
- [Java]Leetcode69 Sqrt(x)