poj2251-Dungeon Master(简单bfs)
来源:互联网 发布:开淘宝店没有营业执照 编辑:程序博客网 时间:2024/05/21 08:46
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 38881 Accepted: 14821
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!
题意:给你一个三维的迷宫,从起点走到终点所需的最短时间,若无法走到则输出Trapped!
分析:在寻常的二维搜索上加一层z轴的搜索就行了。
ac代码如下:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace std;int n, m, t;int dir[][2] = {1,0,-1,0,0,1,0,-1};int fl[2] = {1,-1};char str[50][50][50];struct node{ int x,y,f,t; node(int xx,int yy,int ff,int tt) { x = xx;y = yy;t = tt;f = ff; }};node s(0,0,0,0),e(0,0,0,0);queue<node>q;bool vis[50][50][50];//是否访问bool check(int x,int y,int f)//检验该店是否合法{ if(x > 0&&y > 0&&str[f][x][y]!='#'&&x <= n&&y <= m&&f >= 1&&f <= t) return true; return false;}bool checkans(node a)//检验是否符合答案{ if(e.x == a.x&&e.y == a.y&&e.f == a.f) return true; return false;}int bfs(node f){ q.push(f);//起点入队 while(!q.empty()) { node F = q.front();q.pop(); if(checkans(F)) return F.t; for(int i = 0;i < 4; i++)//进行二维方向的搜索 { int xx = F.x+dir[i][0]; int yy = F.y+dir[i][1]; int ff = F.f; int tt = F.t+1; if(check(xx,yy,ff)&&!vis[ff][xx][yy]) { vis[ff][xx][yy] = true; q.push(node(xx,yy,ff,tt)); } } for(int i = 0;i < 2; i++)//进行z轴方向的搜索 { int xx = F.x; int yy = F.y; int ff = F.f + fl[i]; int tt = F.t+1; if(check(xx,yy,ff)&&!vis[ff][xx][yy]) { vis[ff][xx][yy] = true; q.push(node(xx,yy,ff,tt)); } } } return 0;}int main(){ while(~scanf("%d%d%d",&t,&n,&m)&&n,m,t) { memset(str,0,sizeof(str)); memset(vis,0,sizeof(vis)); for(int k = 1;k <= t; k++) for(int i = 1;i <= n; i++) for(int j = 1;j <= m; j++) { scanf(" %c",&str[k][i][j]); if(str[k][i][j] == 'E') { e.x = i;e.y = j;e.f = k; } if(str[k][i][j] == 'S') { s.x = i;s.y = j;s.f = k; } } while(!q.empty())q.pop(); int ans=0; if(ans = bfs(s)) cout<<"Escaped in "<<ans<<" minute(s)."<<endl; else cout<<"Trapped!"<<endl; } return 0;}
阅读全文
0 0
- poj2251 Dungeon Master 简单BFS
- poj2251-Dungeon Master(简单bfs)
- 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)
- 配置Java环境变量
- 分页
- poj 1011 Sticks
- 数据库性能优化详解
- 电路学习与实验
- poj2251-Dungeon Master(简单bfs)
- 解决Ubuntu16下根目录100%的问题
- 多线程下载
- 初学遗传算法
- 6.重建二叉树
- iOS组件化实践(基于CocoaPods)
- 手把手教你把Vim改装成一个IDE编程环境(图文)
- 嵌入式发展之路
- Week01_day01 JDK安装及环境配置