Dungeon Master (三维BFS)
题目:
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! 题意:
相当于一栋大楼里面很多秘密通道,S是起始位置,E是终点位置,‘#’是墙,‘.’是路,问从S出发最少经过多长时间就到达E处; 分析:
和迷宫不同的是,迷宫是平面上东南西北的移动,相当于在大楼里面的一层楼里找出口,而这个题目在迷宫的基础上又增加了上下的移动,即大楼里面的上下层之间的移动,
所以需要建立三维的数组,找到S的位置,移动方向由4个增加到6个,直到找到E为止,如果找遍了所有的能走的地方都没找到出口E,就出不来了!!! AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<string>
using namespace std;
char a[][][];
int b[][][];
int L,R,C;
int f[][]={{,-,, ,, },
{, ,,-,, },
{, ,, ,,-}};
int s2[][][];
int flag;
struct Knot
{
int x,y,z;
int step;
};
Knot c,d;
bool search1(int x,int y,int z)
{
return (x>=&&x<=L&&y>=&&y<=R&&z>=&&z<=C);
}
int bfs(int si,int sj,int sk)
{
queue<Knot>s;
c.x=si;
c.y=sj;
c.z=sk;
c.step=;
s.push(c);
while (!s.empty())
{
d=s.front();
s.pop();
c.step=d.step+;
for (int i=;i<;i++)
{
c.x=d.x+f[][i];
c.y=d.y+f[][i];
c.z=d.z+f[][i];
if (search1(c.x,c.y,c.z)&&!b[c.x][c.y][c.z]&&a[c.x][c.y][c.z]!='#')
{
if (a[c.x][c.y][c.z]=='E')
return c.step;
b[c.x][c.y][c.z]=;
s.push(c);
}
}
}
return -;
}
int main()
{
int i,j,k;
int si,sj,sk;
while (cin>>L>>R>>C&&(L!=||R!=||C!=))
{
memset(b,,sizeof(b));
for (i=;i<=L;i++)
for (j=;j<=R;j++)
for (k=;k<=C;k++)
{
cin>>a[i][j][k];
if (a[i][j][k]=='S')
{
si=i;
sj=j;
sk=k;
}
}
flag=bfs(si,sj,sk);
if (flag==-)
cout << "Trapped!" << endl;
else
cout << "Escaped in " << flag << " minute(s)." << endl; }
return ;
}
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 ...
- Dungeon Master(三维bfs)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- UVa532 Dungeon Master 三维迷宫
学习点: scanf可以自动过滤空行 搜索时要先判断是否越界(L R C),再判断其他条件是否满足 bfs搜索时可以在入口处(push时)判断是否达到目标,也可以在出口处(pop时) #i ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- 棋盘问题(DFS)& Dungeon Master (BFS)
1棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的 ...
- Dungeon Master (简单BFS)
Problem Description You are trapped in a 3D dungeon and need to find the quickest way out! The dunge ...
- POJ 2252 Dungeon Master 三维水bfs
题目: http://poj.org/problem?id=2251 #include <stdio.h> #include <string.h> #include <q ...
随机推荐
- drf二次封装response-APIViews视图家族-视图工具集-工具视图-路由组件
视图类传递参数给序列化类 (1).在视图类中实例化 序列化对象时,可以设置context内容. (2).在序列化类中的局部钩子.全局钩子.create.update方法中,都可以用self.conte ...
- mysql按月分表, 组合查询
每个月月底最后一天建好下个月的空表 或每年底建1到12月的空表 , table_201901,table_201902,table_201903 增加记录不需要修改,insert到当月对应表就好了. ...
- python文件读写 文件修改
#设置一个变量f为文件对象,并打开文件#写文件#f = open('user.txt','w',encoding='utf-8') #f是一个文件对象f=open(r'c:\Users\PL\Desk ...
- shell_clean_log
apache日志每天进行轮转: vim /usr/local/apache2/conf/extar/httpd-vhosts.conf...ErrorLog "| /usr/local/ap ...
- Pay Back(模拟)
链接:https://ac.nowcoder.com/acm/contest/1086/C 题目描述 "Never a borrower nor a lender be." O h ...
- 三十五、lamp经典组合搭建
一.安装mysql数据库 1.1 创建组和用户: 1)groupadd mysql 2)useradd mysql -g mysql -M -s /sbin/nologin 3)config ...
- 37)PHP,获取数据库值并在html中显示(晋级2)
下面的是上一个的改进版,我知道为啥我的那个有问题了,因为我的__construct()这个函数的里面的那个变量名字搞错了,哎,这是经常犯得毛病,傻了吧唧,气死我了. 之前的那个变量的代码样子: cla ...
- linux打开和关闭端口
查看哪些端口被打开 netstat -anp 关闭端口号:iptables -A INPUT -p tcp -drop 8080 -j DROP 打开端口号:iptables -A INPUT -p ...
- Mock测试,何去何从
2016-10-24 出处:Qtest之道 作/译者:闫耀珍 上面的情景是不是似曾相识呢?现今的业务系统已经很少是孤立存在的了,尤其对于一个大公司而言,各个部门之间的配合非常密切,我们或多或 ...
- iOS漂亮的Toolbar动画、仿美团主页、简易笔记本、流失布局、标签分组等源码
iOS精选源码 JPLiquidLayout 简单易用的流式布局 labelGroupAndStreamSwift---标签分组,单选,多选 iOS采用UITableView和UIScrollView ...