POJ2251:Dungeon Master(BFS)
来源:互联网 发布:java web断点续传 编辑:程序博客网 时间:2024/04/25 09:51
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,直接AC...
<pre name="code" class="cpp">#include<stdio.h>#include<queue>#include<string>using namespace std;char map[32][32][32];int vis[32][32][32];int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};int l,r,c;int sx,sy,sz,ex,ey,ez;struct node{int x,y,z,step;};int check(int x,int y,int z){if(x<0||x>=l||y<0||y>=r||z<0||z>=c||vis[x][y][z]||map[x][y][z]=='#')return 1;return 0;}int bfs(){node a,nex;queue<node>Q;memset(vis,0,sizeof(vis));a.x=sx; a.y=sy; a.z=sz;a.step=0;vis[sx][sy][sz]=1;Q.push(a);while(!Q.empty()){a=Q.front();Q.pop();if(a.x==ex&&a.y==ey&&a.z==ez)return a.step;for(int i=0;i<6;++i){nex=a;nex.x+=dir[i][0];nex.y+=dir[i][1];nex.z+=dir[i][2];if(check(nex.x,nex.y,nex.z))continue;nex.step++;vis[nex.x][nex.y][nex.z]=1;Q.push(nex);}}return -1;}int main(){//freopen("testin.txt","r",stdin); //freopen("testout.txt","w",stdout);while(scanf("%d%d%d",&l,&r,&c),l+r+c){for(int i=0;i<l;++i)for(int j=0;j<r;++j){scanf("%s",map[i][j]);for(int k=0;k<c;++k){if(map[i][j][k]=='S') {sx=i;sy=j;sz=k;}if(map[i][j][k]=='E') {ex=i;ey=j;ez=k;}}}int ans=bfs();if(ans>0)printf("Escaped in %d minute(s).\n",ans);elseputs("Trapped!");}return 0;}
0 0
- poj2251 Dungeon Master (BFS)
- Dungeon Master(poj2251,bfs)
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- [bfs]poj2251 Dungeon Master
- poj2251 dungeon master【BFS】~
- POJ2251 Dungeon Master(bfs)
- POJ2251 Dungeon Master(BFS)
- POJ2251-Dungeon Master-BFS
- 【POJ2251】Dungeon Master(bfs)
- poj2251-bfs- Dungeon Master
- POJ2251 Dungeon Master 【BFS】
- POJ2251:Dungeon Master(BFS)
- Dungeon Master(poj2251,bfs)
- POJ2251 Dungeon Master 三维BFS
- poj 2287 Tian Ji -- The Horse Racing (贪心)
- eclipse同名变量高亮显示设置方法
- 【翻译自mos文章】怎么检查Oracle GoldenGate(OGG)的checkpoint file 是否被locked?
- hdu 1086 You can Solve a Geometry Problem too(求线段相交点个数 模板)
- 撒旦法撒旦法撒
- POJ2251:Dungeon Master(BFS)
- ImageView.ScaleType 设置图解
- 小餐馆被改变星辰变
- 鞋柜的粉红色东风公司
- C++ primer 第十章 文本查询程序
- 一秒去除Win7快捷方式箭头 亲试可以忘记转载来源了
- 阿里巴巴2015年校招内推一面
- 两个月没写博客了
- VS2013编译C++程序闪烁即消失