【POJ2251】Dungeon Master
本题知识点:宽度优先搜索
题意简单。在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径。每走一格每上或者下一层都算1步。
一开始以为这个“立体迷宫”有点吓到我(题做得太少了),后来发觉,只是一个三维数组以及多了两个操作方向(原地向上或者原地向下),除此之外就是简单的bfs模板题了。
数据很小。
// POJ 2251
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
// R == H, C == W
int L, R, C;
char maze[35][35][35];
int len[35][35][35];
bool stay[35][35][35];
int bl, br, bc, el, er, ec;
int rl[] = { 0, 0, 0, 0, 1, -1 };
int rc[] = { 1, -1, 0, 0, 0, 0 };
int rr[] = { 0, 0, 1, -1, 0, 0 };
struct node{
int l, r, c;
};
queue<node> que;
void build(){
for(int i = 0; i < L; i++){
for(int j = 0; j < R; j++){
scanf("%s", maze[i][j]);
// 找起始点
for(int k = 0; k < C; k++){
if(maze[i][j][k] == 'S'){
bl = i; br = j; bc = k;
}
if(maze[i][j][k] == 'E'){
el = i; er = j; ec = k;
}
}
}
}
// 注意队列一定要清空!
while(!que.empty()) que.pop();
}
void show(){
cout << "len\n";
for(int i = 0; i < L; i++){
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
printf("%d ", len[i][j][k]);
} cout << endl;
} cout << endl;
} cout << endl;
}
void bfs(){
node a;
a.l = bl; a.r = br; a.c = bc;
len[bl][br][bc] = 0;
stay[bl][br][bc] = true;
que.push(a);
while(!que.empty()){
node now = que.front(), next; que.pop();
if(now.l == el && now.r == er && now.c == ec){
break;
}
// 这里比较写的有点花
for(int i = 0; i < 6; i++){
next.l = now.l + rl[i]; next.r = now.r + rr[i]; next.c = now.c + rc[i];
if(0 <= next.l && next.l < L && 0 <= next.r && next.r < R && 0 <= next.c && next.c < C
&& maze[next.l][next.r][next.c] != '#' && !stay[next.l][next.r][next.c]){
stay[next.l][next.r][next.c] = true;
len[next.l][next.r][next.c] = len[now.l][now.r][now.c] + 1;
que.push(next);
}
}
}
}
int main()
{
while(scanf("%d %d %d", &L, &R, &C) && L + R + C != 0){
memset(stay, false, sizeof(stay));
memset(len, -1, sizeof(len));
build();
// bfs
bfs();
// show();
if(len[el][er][ec] != -1)
printf("Escaped in %d minute(s).\n", len[el][er][ec]);
else printf("Trapped!\n");
}
return 0;
}
//3 3 3
//S.#
//###
//###
//
//#.#
//###
//###
//
//#E#
//###
//###
【POJ2251】Dungeon Master的更多相关文章
- 【搜索】Dungeon Master
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 【SaltStack】通过Master给Minion安装MySQL
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 【SaltStack】在Master上给Minion端安装zabbix
一.IP信息说明 [Master] IP: 192.168.236.100 [Minion] IP: 192.168.236.101 二.配置SaltStack 关于SaltStack Master和 ...
- 【POJ 2251】Dungeon Master(bfs)
BUPT2017 wintertraining(16) #5 B POJ - 2251 题意 3维的地图,求从S到E的最短路径长度 题解 bfs 代码 #include <cstdio> ...
- 【leetcode】Dungeon Game
Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...
- 【leetcode】Dungeon Game (middle)
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- 【转】Spark:Master High Availability(HA)高可用配置的2种实现
原博文出自于: 感谢! Spark Standalone集群是Master-Slaves架构的集群模式,和大部分的Master-Slaves结构集群一样,存在着Master单点故障的问题.如何解决这个 ...
- 【摘录】JDBC Master Slave(JDBC方式的JMS集群)
JDBC Master Slave First supported in ActiveMQ version 4.1 If you are using pure JDBC and not using t ...
随机推荐
- Windows下使用MongoDb的经验
随着NoSql广泛应用MongoDb这个Json数据库现在也被广泛使用,接下来简单介绍一下Windows下如使安装使用MongoDb. 一.安装MongoDb 1.首先去官方网址:(https://w ...
- python 工厂方法
工厂方法模式(FACTORY METHOD)是一种常用创建型设计模式,此模式的核心精神是封装类中变化的部分,提取其中个性化善变的部分为独立类, 通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. ...
- 关于创建Web图像时应记住的五个要素
1. 格式与下载速度 当前,Web上用的最广泛的三种格式是GIF.PNG和JPEG.我们的目标是选择质量最高,同时文件最小的格式. WebP图像格式 谷歌建立了另一种图像格式,名为WebP. 这种格式 ...
- dede自定义内容模型下,列表只显示10条的问题及解决方法
<div class="zjtd-content-ld s-content"> {dede:arclist tagid='ld' row='100' pagesize= ...
- FI-BTEs增强FIBF
https://wenku.baidu.com/view/8a31a4bafd0a79563c1e72f6.html 1.事务码FIBF 2.激活
- Spring 在xml文件中配置Bean
Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...
- C# DataTable 和List之间相互转换的方法(转载)
来源:https://www.cnblogs.com/shiyh/p/7478241.html 一.List<T>/IEnumerable转换到DataTable/DataView 方法一 ...
- centos7 docker安装nginx
1.查询nginx最新镜像 docker search nginx 2.下载镜像 docker pull nginx 3.创建目录 mkdir -p /software/nginx/html mkdi ...
- 计算地图上两点间的距离PHP类
计算地图上两点间的距离,使用的是谷歌地图 <?php class GeoHelper { /** * @param int $lat1 * @param int $lon1 * @param i ...
- docker容器监控:cadvisor+influxdb+grafana
cadvisor+influxdb+grafana可以实现容器信息获取.存储.显示等容器监控功能,是目前流行的docker监控开源方案. 方案介绍 cadvisor Google开源的用于监控基础设施 ...