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 ...
随机推荐
- 关于Algorithm in Nutshell源代码
获取源码包 "Algorithm in Nutshell"的源码包ADK-1.0.zip在这本书主页上"Download Example Code"页面的Rel ...
- WPF 图表控件之曲线绘制与移动
目的:绘制简单轻量级的曲线视图 二.实现效果: 1,绘制标准基准线 2,可拖动 三.用到控件 1,Canvas 2,Ellipse XAML代码: <Canvas Background=&quo ...
- 1051 Pop Sequence (25分)栈
刷题 题意:栈的容量是5,从1~7这7个数字,写5个测试数据 做法:模拟栈 #include<bits/stdc++.h> using namespace std; const int m ...
- 用kivy学习制作简易调色画板app
制作一款简易的调色画板,要用到的知识:页面布局.ToggleButton.ToggleButtonBehavior.get_color_from_hex(兼容十六进制编码颜色):功能上要可以选择颜色, ...
- ASP.NET Core导入导出Excel文件
ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...
- 内网隧道与SOCKS代理思路总结
早就想总结一下这部分内容了,总是有其他事情卡住,一直拖到现在 内网中有很多边界设备,比如防火墙.这种边界设备会控制内部主机的对外连接,一般会仅允许某些种类的端口开放或某种数据流量出入 这就意味着我们只 ...
- WooYun虚拟机的搭建及配置方法
"当时代需要时,他们勇敢地站了出来.当潮水褪去时,他们等待新的使命.他们等待被抛弃或者被怀念,等待这个世界告诉他们善恶和对错." 记录一下复活乌云的过程. 第一步:虚拟机下载 网上 ...
- Django 模版语法 测试环境 ORM单表查询
模版语法 传值 视图函数向前端html页面传值,基本上所有的数据类型都可以渲染在前端页面上. views.py from django.shortcuts import render, redirec ...
- 基于Python的决策树分类器与剪枝
作者|Angel Das 编译|VK 来源|Towards Data Science 介绍 决策树分类器是一种有监督的学习模型,在我们关心可解释性时非常有用. 决策树通过基于每个层次的多个问题做出决策 ...
- NOIP 模拟 $30\; \rm 毛二琛$
题解 \(by\;zj\varphi\) 原题问的就是对于一个序列,其中有的数之间有大小关系限制,问有多少种方案. 设 \(dp_{i,j}\) 表示在前 \(i\) 个数中,第 \(i\) 个的排名 ...