POJ2251——Dungeon Master(三维BFS)
和迷宫问题区别不大,相比于POJ1321的棋盘问题,这里的BFS是三维的,即从4个方向变为6个方向。
用上队列的进出操作较为轻松。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std; char map[35][35][35];
int vis[35][35][35];
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int k,n,m,sx,sy,sz,ex,ey,ez;
struct node
{
int x,y,z,step;
};
int check(int x,int y,int z)
{
if(x<0||y<0||z<0||x>=k||y>=n||z>=m||map[x][y][z]=='#'||vis[x][y][z])
return 1;
return 0;
}
int bfs()
{
int i;
node a,next;
queue<node> Q;
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(i=0;i<6;i++)
{
next=a;
next.x=a.x+to[i][0];
next.y=a.y+to[i][1];
next.z=a.z+to[i][2];
if(check(next.x,next.y,next.z))
continue;
vis[next.x][next.y][next.z]=1;
next.step=a.step+1;
Q.push(next);
}
}
return 0;
}
int main()
{
int i,j,r;
while(scanf("%d%d%d",&k,&n,&m),n+m+k)
{
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
scanf("%s",map[i][j]);
for(r=0;r<m;r++)
{
if(map[i][j][r]=='S')
{
sx=i,sy=j,sz=r;
}
else if(map[i][j][r]=='E')
{
ex=i,ey=j,ez=r;
}
}
}
}
memset(vis,0,sizeof(vis));
int ans;
ans=bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}
POJ2251——Dungeon Master(三维BFS)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 2251 Dungeon Master (三维BFS)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- ZOJ 1940 Dungeon Master 三维BFS
Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Desc ...
- POJ-2251 Dungeon Master (BFS模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- POJ2251 Dungeon Master —— BFS
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- BFS POJ2251 Dungeon Master
B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
随机推荐
- 接口自动化测试之httprunner初探
❝ 今天来介绍一个python的一个开源项目:httprunner,接口自动化工具.第一次输入,难免有不周到的地方,轻喷~ ❞ 介绍: HttpRunner是一个简单优雅但功能强大的 HTTP(S) ...
- TortoiseSVN日志字体和字号调整
TortoiseSVN提供的"show log"功能很有用,但默认的显示文件log历史的字体太小看不清,这个字体的设置在[TortoiseSVN ->Settings-> ...
- 计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场
计算机课程设计-校园二手书交易系统java二手交易平台代码ssm二手商城购物平台跳蚤市场 注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SSM(S ...
- 003 PCI Express体系结构(三)
一.PCI总线的存储器读写总线事务 总线的基本任务是实现数据传送,将一组数据从一个设备传送到另一个设备,当然总线也可以将一个设备的数据广播到多个设备.在处理器系统中,这些数据传送都要依赖一定的规则,P ...
- Java MyEclipse:The type java.lang.CharSequence cannot be resolved. It is indirectly referen
从svn上下载项目后配置weblogic后启动报错: myeclipse The type java.lang.CharSequence cannot be resolved. It is indi ...
- js函数聚合
//掺杂类实现聚合(有的时候,我们需要某个或多个类里的一些方法函数) //将要被聚合的函数 var JSON = { toJsonString: function () { var output = ...
- msql中@RequestParam、@Param、@PathVariable的用法
@RequestParam的用法 1.可以对传入参数指定参数名,将请求参数绑定至方法参数 // 下面的对传入参数指定为aa,如果前端不传aa参数名,会报错 @RequestParam(value=&q ...
- java String数组城市
String[] citys = {"北京","天津","河北","山西","内蒙古"," ...
- 同步(synchronized)
转载至:https://www.cnblogs.com/dolphin0520/p/3923737.html 一.什么时候会出现线程安全问题? 在单线程中不会出现线程安全问题,而在多线程编程中,有可能 ...
- 在多数据源中对部分数据表使用shardingsphere进行分库分表
背景 近期在项目中需要使用多数据源,其中有一些表的数据量比较大,需要对其进行分库分表:而其他数据表数据量比较正常,单表就可以. 项目中可能使用其他组的数据源数据,因此需要多数据源支持. 经过调研多数据 ...