【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 ...
随机推荐
- 没有15k薪资都不会了解的测试内幕
软件测试的工程师阶层是指随着行业的飞速发展,测试人员犹如身在洪流之中“逆水行舟不进则退”.知其然已经无法满足当今的测试人员,还要知其所以然.所以测试人员不仅仅要关注系统外部结构,还得了解系统内部的逻辑 ...
- docker安装mysql笔记
首先 查找镜像 docker search mysql 拉取镜像 : docker pull mysql 拉取成功后,查看本地镜像: docker images 可以看到本地有两个镜像(redis是我 ...
- RocketMQ在CentOS7上安装
需要先以下组件 1.64bit OS, Linux/Unix/Mac is recommended;2.64bit JDK 1.8+; 安装了 Java JDK 就可以运行 RocketMQ 了 3. ...
- StringUtils系列之StringUtils.isNotBlank()和StringUtils.isNotBlank()的区别
/** 1. * StringUtils.isNotBlank(); * 判断参数是否不为空. * 1.如果不为空返回true. * 2.如果为空返回false. * StringUtils.isNo ...
- 如何用 DHCP + DNS + Web 实现一个网络架构
为什么?为什么?为什么在浏览器里输入www.baidu.com就可以访问百度搜索,而输入pan.baidu.com就可以访问百度网盘,它是怎么实现的? 实验原理简介 在Internet中,计算机之间通 ...
- k8s之Configmap与Secret
ConfigMap:k8s标准资源,将配置文件做成k8s资源,使其它资源可加载其中配置 Secret:实现加密功能的安全配置文件.由多个key:val中组成 创建configmap资源,可直接使用ku ...
- Ubuntu安装Gitlab Runner
第一步: 添加GitLab的官方存储库: curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runne ...
- varnish加速web
varnish主要功能是代理服务器和缓存,和nginx代理一样可以反向代理web服务器.但是varnish不能作为web服务器.但是它的优点是可以将访问过的网站还存在代理服务器上,以便于提升web的访 ...
- web程序防止攻击的一些资料——整理
地址:https://docs.microsoft.com/en-us/previous-versions/aspnet/a2a4yykt(v=vs.100)?redirectedfrom=MSDN ...
- Python使用pip安装TensorFlow模块
1.首先确保已经安装python,然后用pip来安装matplotlib模块. 2.进入到cmd窗口下,建议执行python -m pip install -U pip setuptools进行升级. ...