Dungeon Master (简单BFS)
Problem Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5
S....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped! 简单的三维BFS,在普通的二维平面图上增加了层数这个维度,除了在同一层东南西北方向上行走之外,还可以在相邻层之间行走,例如:可以从(x,y,0)---->(x,y,1),读懂题意后就很好写了,BFS模板走起
#include <algorithm>
#include <bitset>
//#include <bits/extc++.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue> using namespace std;
//using namespace __gnu_pbds #define ll long long
#define maxn 105 char maps[][][];
int dir[][] = {{, , }, {-, , }, {, , }, {, -, }, {, , }, {, , -}}, step[][][];
bool vis[][][];
int L, R, C; struct Node
{
int l, r, c;
} start, end1; bool check(Node x)
{
if (x.l < || x.l >= L || x.r < || x.r >= R || x.c < || x.c >= C || !vis[x.l][x.r][x.c] || maps[x.l][x.r][x.c] == '#')
{
return false;
}
return true;
} void bfs()
{
queue<Node> q;
q.push(start);
step[start.l][start.r][start.c] = ;
vis[start.l][start.r][start.c] = false;
while (!q.empty())
{
Node now = q.front();
q.pop();
for (int i = ; i < ; ++i)
{
Node next = now;
next.l += dir[i][];
next.r += dir[i][];
next.c += dir[i][];
// cout << next.l << " " << next.r << " " << next.c << endl;
// cout << maps[next.l][next.r][next.c] << endl;
if (check(next))
{
//cout << next.l << " " << next.r << " " << next.c << endl;
step[next.l][next.r][next.c] = step[now.l][now.r][now.c] + ;
vis[next.l][next.r][next.c] = false;
q.push(next);
}
}
}
} int main()
{
while (scanf("%d%d%d", &L, &R, &C) && L && R && C)
{
memset(step, -, sizeof(step));
memset(vis, true, sizeof(vis));
for (int i = ; i < L; ++i)
{
for (int j = ; j < R; ++j)
{
for (int k = ; k < C; ++k)
{
scanf(" %c", &maps[i][j][k]);
if (maps[i][j][k] == 'S')
{
start.l = i;
start.r = j;
start.c = k;
}
else if (maps[i][j][k] == 'E')
{
end1.l = i;
end1.r = j;
end1.c = k;
}
}
}
}
bfs();
if (step[end1.l][end1.r][end1.c] != -)
{
printf("Escaped in %d minute(s).\n", step[end1.l][end1.r][end1.c]);
}
else
{
puts("Trapped!");
}
}
return ;
}
Dungeon Master (简单BFS)的更多相关文章
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- 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)
题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- (简单) POJ 2251 Dungeon Master,BFS。
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)
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模板题)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
随机推荐
- Java基础系列8——IO流超详细总结
该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 在初学Java时,I ...
- vue-learning:12-1- HTML5的<template>内容模板元素
HTML5的<template>内容模板元素 HTML内容模板<template>元素将它其中的内容存储在页面文档中,以供后续使用,该内容的DOM结构在加载页面时会被解析器处理 ...
- 2018-8-10-win10-uwp-Window.Current.Dispatcher中Current为null
title author date CreateTime categories win10 uwp Window.Current.Dispatcher中Current为null lindexi 201 ...
- 很多.net 程序员不知道又非常重要的 .net高级调试技巧.调试别人的dll方法内的变量
事情是这样的, 最近需要开发Orcale的数据库. 于是使用了EF 加上 Oracle.ManagedDataAccess.Client 这个Oracle.ManagedDataAccess 很好用, ...
- java中获取url传值时的int类型参数的方法
int parameterName=Integer.valueOf(request.getParameter("你所要获得的int类型的参数名"));
- 【题解】有标号的DAG计数1
[HZOI 2015] 有标号的DAG计数 I 设\(f_i\)为\(i\)个点时的DAG图,(不必联通) 考虑如何转移,由于一个DAG必然有至少一个出度为\(0\)的点,所以我们钦定多少个出度为\( ...
- Navicat Premium连接Oracle数据库
记录一下本次配置过程中遇到的问题: 一.服务名 1.找到tnsnames.ors文件: 二.Navicat自带的oci.dll文件版本可能和Oracle数据库不一致,所以使用oracle自带的oci. ...
- Redis-NoSQL入门和概述(一)
NoSQL简史及定义 NoSQL 这个术语最早是在 1998 年被Carlo Strozzi命名在他的轻量的,开源的关系型数据库上的,但是该数据库没有提供标准的SQL接口:在2009 年再次被Eric ...
- VMware 完成 27 亿美元的 Pivotal 收购 | 云原生生态周报 Vol. 34
作者 | 汪萌海.王思宇.李鹏 业界要闻 VMware 完成 27 亿美元的 Pivotal 收购 VMware 在 12 月 30 日宣布,已完成 27 亿美元的 Pivotal 收购,同一天 Pi ...
- vim的常用指令(脑图)
将正在编辑的文件另存新文件名 :w newfilename 在正在编辑的文件中,读取一个filename :r filename 做了很多编辑工作,想还原成原来的文件内容 :e! 我在v ...