【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 ...
随机推荐
- 3.matplotlib绘制条形图
plt.bar() # coding=utf-8 from matplotlib import pyplot as plt from matplotlib import font_manager my ...
- js获取任意一天的0点和23:59:59时间
最近写代码时,需要获取任意一天的起始和结束时间,0点和23:59:59这两个时间的时间戳 使用了setHours() 方法 setHours() 方法用于设置指定的时间的小时字段 1. 获取当天开始时 ...
- iOS中copy和strong修饰符的区别
iOS中copy和strong修饰符的区别 //用copys修饰的生成的都是不可变的对象 ,如果调用可变类型方法的直接报错 @property(nonatomic,copy)NSString * cp ...
- Centos7.3安装nexus-3.14.0-04
nexus-3.14.0-04的安装 nexus-3.14.0-04-unix.tar.gz 1.下载nexus 2.上传到服务器/root ...
- Android开发之常用Intent.Action【转】
1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); intent.putEx ...
- Python的路径操作(os模块与pathlib模块)
Python的路径操作(os模块与pathlib模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.os.path模块(Python 3.4版本之前推荐使用该模块) #!/u ...
- 基于kubeamd初始化安装kubernetes集群
环境:master,etcd 172.16.1.5node1 172.16.1.6node2 172.16.1.7前提:1.基于主机名通信,/etc/hosts 2.时间同步3.关闭firewalld ...
- Matplotlib:mpl_toolkits.mplot3d工具包
简介 mpl_toolkits.mplot3d是Matplotlib里面专门用来画三维图的工具包,官方指南请点击此处<mplot3d tutorial> 使用 导入 使用from mpl_ ...
- Python模块(导入,内置,自定义,开源)
目录: 模块介绍 自定义模块 内置模块 开源模块 一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用p ...
- jquery 内容筛选选择器
基本筛选选择器针对的都是元素DOM节点,如果我们要通过内容来过滤,jQuery也提供了一组内容筛选选择器,当然其规则也会体现在它所包含的子元素或者文本内容上 注意事项: :contains与:has都 ...