2251 Dungeon Master【bfs】
来源:互联网 发布:php 汉字转ascii 编辑:程序博客网 时间:2024/06/05 00:26
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20969 Accepted: 8136
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 题目,这个题看起来比较恐怖,因为是三维立体的,但是也不算复杂,因为在这个图里,没有特别复杂的限定条件,到达下一步的时间都一样,这样只需要简单的处理就可以了,不过注意标记,以及入队元素的判断,
bfs 优先本层搜索,之后在进行更深一层的搜索...
#include<stdio.h>#include<queue>#include<string.h>using namespace std;char x[50][50][50];int l,r,c,v[50][50][50],kase;int fx[4]={0,-1,1,0},fy[4]={-1,0,0,1},fz[2]={-1,1};//这样定义数组有利于后面操作struct ex{int x,y,z;//三个坐标int t;//用的时间}w,tp;queue<ex> q;void bfs(){while(!q.empty()){w=q.front();q.pop();if(x[w.x][w.y][w.z]=='E')//到达出口{kase=1;return;}for(int i=0;i<2;++i)//这里考虑的是上下两个方向{int tx=w.x,ty=w.y,tz=w.z+fz[i];tp=w;if(tz<0||tz>=c||x[tx][ty][tz]=='#'){continue;}if(!v[tx][ty][tz])//判断是否要入队{tp.z=tz;++tp.t;q.push(tp);v[tx][ty][tz]=1;}}for(int i=0;i<4;++i)//这里考虑前后左右的方向.{int tx=w.x+fx[i],ty=w.y+fy[i],tz=w.z;tp=w;if(tx<0||tx>=l||ty<0||ty>=r||x[tx][ty][tz]=='#')//越界或者不能行走{continue;}if(!v[tx][ty][tz])//判断状态是否出现过{tp.x=tx;tp.y=ty;++tp.t;q.push(tp);v[tx][ty][tz]=1;}}}}int main(){int i,j,k,a,b,d;char s;while(scanf("%d%d%d",&l,&r,&c),l|r|c){for(i=0;i<l;++i){for(j=0;j<r;++j)//输入要注意,格式的控制{getchar();for(k=0;k<c;++k){scanf("%c",&s);x[i][j][k]=s;if(s=='S'){a=i;b=j;d=k;}}}getchar();}memset(v,0,sizeof(v));while(!q.empty())//注意清空队列{q.pop();}w.x=a;w.y=b;w.z=d;w.t=0;v[a][b][c]=1;kase=0;q.push(w);//入队第一个元素bfs();if(kase)//满足条件{printf("Escaped in %d minute(s).\n",w.t);continue;}printf("Trapped!\n");}return 0;}
0 0
- (BFS)2251 Dungeon Master
- 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】
- 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)
- HDU 5353 Average(枚举)
- LeetCode OJ - - Edit Distance
- 杭电 ACM 1008:Elevator
- Oracle事务的隐式提交
- 解析Myeclipse项目下的.classpath文件
- 2251 Dungeon Master【bfs】
- Linux 下常用的 19 条命令
- C语言操作windows的小程序
- 谈谈基类与子类的this指针(C++)
- [USACO2015February,Bronze] Problem2.COW
- 有符号变量与无符号变量的值的转换
- 分布式交易系统设计 7 分布式锁
- Leetcode# 98 Validate Binary Search Tree
- Java动态代理与AOP概念