openjudge2.5基本算法之搜索1253:Dungeon Master

题目

1253:Dungeon Master
总时间限制: 1000ms 内存限制: 65536kB
描述
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?
输入
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.
输出
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!
样例输入
3 4 5
S…
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0
样例输出
Escaped in 11 minute(s).
Trapped!

翻译

你被困在trapped一个3D地下城dungeon ,需要找到最快的出路!地下城是由单元立方体cubes 组成的,
这些立方体可能被石头填满,也可能没有。移动一个单位需要一分钟,
北,南,东,西,上或下。你不能沿对角线diagonally 移动,迷宫四面都是坚固的岩石。
有可能逃脱吗?如果是,需要多长时间?

输入包括许多地下城。每个地下城的描述都以包含三个整数L、R和C的一行开头(大小都限制在30个以内)。
L是构成地下城的关卡数。
R和C是构成每一层平面的行数和列数。
然后是L块的R行,每一行包含C个字符。每个角色描述describes 地下城的一个牢房。
充满岩石的单元格用“#”表示indicated ,
空单元格用“.”表示represented 。
起始位置用“S”表示,
退出位置用字母“E”表示。
每个关卡后都有一个空白行blank 。输入端以L、R和C的三个0结束terminated。

每个迷宫maze 产生generates 一行输出。如果有可能到达出口,打印一行表格
在x分钟内转义。
其中x用逃逸所需的最短时间代替。
如果无法转义,则打印该行
被困!

理解

用宽搜就可以,当然是最早到达的就是。
提取队列第一个元素,将能到达的点放进队列,直到到达目的地。
区别就是上下左右六个点外,还可以是上下。共八种移动。

代码

#include <bits/stdc++.h>
using namespace std;
int l,r,c,
d[6][3]={{0,-1,0},{1,0,0},{0,1,0},{-1,0,0},{0,0,-1},{0,0,1}},
k[35][35][35];//到达该点的步数
char dungeons[35][35][35];
struct node{
int l,x,y;
};
queue q;//宽搜队列
bool ok(int lx,int xx,int yx){
//判断在地宫里,并且该点能走且没走过
return lx>=1&&lx<=l&&xx>=1&&xx<=r&&yx>=1&&yx<=c&&dungeons[lx][xx][yx]!=‘#’&&!k[lx][xx][yx];
}
int main(){
//freopen(“data.cpp”,“r”,stdin);
while(cin>>l>>r>>c&&(l||r||c)){
memset(k,0,sizeof(k));
while(!q.empty())q.pop();
for(int w=1;w<=l;w++)
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++){
cin>>dungeons[w][i][j];
if(dungeons[w][i][j]= =‘S’){
q.push(node{w,i,j});
}
}
bool kk=0;int ans;
while(!q.empty()){
int l1=q.front().l,x1=q.front().x,y1=q.front().y;
q.pop();
if(dungeons[l1][x1][y1]==‘E’){
kk=1;ans=k[l1][x1][y1];
break;
}
for(int i=0;i<6;i++){
int l,x,y;
l=l1+d[i][2], x=x1+d[i][0], y=y1+d[i][1];
if(ok(l,x,y)){
k[l][x][y]=k[l1][x1][y1]+1;
q.push(node{l,x,y});
}
}
}
if(kk)cout<<“Escaped in “<<ans<<” minute(s).”<<endl;
else cout<<“Trapped!”<<endl;

}
return 0;

}

小结

不能胆小,战战兢兢一事无成。战略上藐视,战术上重视!

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值