POJ 2251Dungeon Master
POJ 2251Dungeon Master
题目链接
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
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
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.
Ouput
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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
Sample Output
解题思路
1.将搜索扩充到三维上,在二维的基础上增加层数,相当于增加了一个分量,基本思路同二维上的搜索,但要注意以下几点(纪念自己WA了N次)
2.深搜会超时,要采用记忆化搜索,所以不如直接用广搜
3.题目的数据量给小了,讨论区里有的数组开到105才AC
4.S点并不一定是第一层最左上角的点
5.bfs时,一点要把队列清空,因为存在下一组数据(所以初始化和清空很重要,有点内存管理的意思,反而忽略了,活该WA了这么多次)
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<cstdio>
#include<memory.h>
using namespace std;
struct Node {//使用结构体便于bfs将点加入队列
int x,y;//点的横纵坐标
int floot;//层数
int setp;//走到当前点的步数
} node,nodes;
queue<struct Node>q;
int a[200][200][200];//存储每层,注意数据范围给小了,尽量开的大一点
bool book[200][200][200];//标记数组
int nexts[6][3]= {{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0},{0,-1,0}};//控制方向
int main() {
int n,m,l;
char c;
while(cin>>l>>n>>m) {
getchar();
if(l==0&&n==0&&m==0) {
break;
}
memset(a,0,sizeof(a));//初始化
memset(book,false,sizeof(book));
for(int i=1; i<=l; i++) {//输入
for(int j=1; j<=n; j++) {
for(int k=1; k<=m; k++) {
cin>>c;
if(c=='.') {
a[i][j][k]=1;
}
else if(c=='E') {
a[i][j][k]=2;
}
else if(c=='S') {
a[i][j][k]=3;
}
}
getchar();
}
getchar();//注意吸收空行
}
int flagg=0;
for(int i=1; i<=l; i++) {//寻找S点,并将其加入队列,开始bfs
for(int j=1; j<=n; j++) {
for(int k=1; k<=m; k++) {
if(a[i][j][k]==3) {
node.x=j;
node.y=k;
node.floot=i;
node.setp=0;
q.push(node);
book[i][j][k]=true;//注意标记
break;
}
}
}
}
while(!q.empty()) {
if(flagg==1) {
break;
}
node=q.front();
q.pop();
for(int f=0; f<6; f++) {//遍历6个方向
int floots=node.floot+nexts[f][0];
int xs=node.x+nexts[f][1];
int ys=node.y+nexts[f][2];
int setps=node.setp+1;
if(a[floots][xs][ys]==2) {//已经到达E
cout<<"Escaped in "<<setps<<" minute(s)."<<endl;
flagg=1;
break;
}
else if(a[floots][xs][ys]==1&&book[floots][xs][ys]==false) {//此点可走
nodes.floot=floots;
nodes.x=xs;
nodes.y=ys;
nodes.setp=setps;
q.push(nodes);
book[floots][xs][ys]=true;//注意标记,否则将超时
}
}
}
while(!q.empty()) {//一定记得清空队列,不然WA到怀疑人生
q.pop();
}
if(flagg==0) {
cout<<"Trapped!"<<endl;
}
}
return 0;
}
上一篇: 144Hz 2K带鱼屏!小米曲面显示器34英寸降至2199元
下一篇: STM32基础定时器详解
推荐阅读
-
mysql (master/slave)复制原理及配置
-
mysql (master/slave)复制原理及配置
-
MySQL-MMM安装指南(Multi-Master Replication Manager for MySQL_MySQL
-
Agri-Net (poj 1258 最短路+prim)
-
【代码超详解】POJ 3126 Prime Path(质数打表 + BFS)
-
poj2312-Battle City
-
POJ2312Battle City
-
POJ3126 Prime Path(BFS) 类似于Leetcode 单词接龙
-
POJ 3126 Prime Path (BFS) (F)
-
POJ 2312:Battle City(BFS)