POJ 2251 Dungeon Master(多层地图找最短路 经典bfs,6个方向)
2024-09-01 23:05:09
Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 48380 | Accepted: 18252 |
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.
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!
Source
跟平常的bfs问题的区别就是方向有6个
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define max_v 35
using namespace std;
int dir[][]={{,,},{,,},{,,-},{,-,},{,,},{-,,}};//6个方向 东,南,西,北,上,下
char G[max_v][max_v][max_v];
int vis[max_v][max_v][max_v];
int sx,sy,sz,fx,fy,fz;
int n,m,k;
int ans;
struct node
{
int x,y,z;
int step;
};
bool check(node a)//检查该点合法性
{
if(a.x<||a.x>=n||a.y<||a.y>=m||a.z<||a.z>=k)
return false;
else if(G[a.z][a.x][a.y]=='#')
return false;
else if(vis[a.z][a.x][a.y])
return false;
else
return true;
}
void bfs(int x,int y,int z)
{
queue<node> q;
node p,next; p.x=x;
p.y=y;
p.z=z;
p.step=; q.push(p); while(!q.empty())
{
p=q.front();
q.pop(); if(p.x==fx&&p.y==fy&&p.z==fz)
{
ans=p.step;
return ;
} for(int i=;i<;i++)
{
next.x=p.x+dir[i][];
next.y=p.y+dir[i][];
next.z=p.z+dir[i][]; if(check(next))
{
vis[next.z][next.x][next.y]=;
next.step=p.step+;
q.push(next);
} }
}
}
int main()
{
while(~scanf("%d %d %d",&k,&n,&m))
{
if(n==&&m==&&k==)
break; for(int z=;z<k;z++)
{
for(int i=;i<n;i++)
{
scanf("%s",G[z][i]);
for(int j=;j<m;j++)
{
if(G[z][i][j]=='S')
sx=i,sy=j,sz=z;
else if(G[z][i][j]=='E')
fx=i,fy=j,fz=z;
}
}
} memset(vis,,sizeof(vis));
ans=-; bfs(sx,sy,sz); if(ans==-)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return ;
}
最新文章
- Java多态性——分派
- 理解DOM事件流的三个阶段
- parawork功能使用说明
- 浅析 IDE跟编译器
- SQLServer中游标是如何处理数据的?
- SpringMVC使用静态资源
- BZOJ 4557 侦查守卫
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
- javascript--”原路返回“
- 在 LINQ to Entities 查询中无法构造实体或复杂类型
- QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?
- Single linked list by cursor
- 程序员的自我救赎---1.4.3: 核心框架讲解(MVC)
- 软件工程(FZU2015) 赛季得分榜,第10回合(alpha冲刺)
- Tomcat7/8访问Server Status、Manager App、Host Manager出现403 forbidden
- 学习笔记: jstack与线程状态
- vuejs实现瀑布流布局(二)
- 搭建eclipse开发环境
- [原创]Fitnesse测试工具介绍及安装
- Vue实例的的data对象