OpenJudge 2.5-1253 地下城大师(Dungeon Master)
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
-
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.
Output
-
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
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!大意:
你被困在3D地下城里,并且需要找到最快的出路!地下城是由单位立方体组成的,它们可能是也可能不是被岩石填满的。(意思就是被岩石填满的地方不是通路,不能走)向东、南、西、北、上或下移动需要花费一分钟。不能对角移动。迷宫四周都是坚硬的岩石。
有可能逃跑吗?如果有,需要花费多少时间?
输入:输入包括若干个地下城,每个地下城的描述都是由L、R和C三个整数(均不超过30)开始,L是地下城的层数。R和C是每一层的行数和列数。接下来L块,每块包括R行,每行C个字符描述地下城。‘#’代表岩石,‘.’代表空地,‘S’表示你的起始位置,‘E’表示出口。输入由L,R,E为3个0结束。
输出:每个迷宫输出一行。如果有可能到达出口,则按格式输出:“Escaped in x minute(s).”;如果无法逃脱,则输出:“Trapped! ”
思路:
这道题乍看有难度,是3D迷宫,有层数,但其实很简单,广搜算法(最快的路),与其它的迷宫无异,只不过所有与坐标相关的变量都要再加一个表示层数,map(记录地下城描述)要用三维数组,移动方向有6个,增加了上下。
我知道你们想看这个:
#include<cstdio>
#include<iostream>
using namespace std;
int head=0,tail=1,nextx,nexty,nextz,l,r,c,getx,gety,getz;
int pre[1000000],a[1000000],b[1000000],cq[1000000]/*这个cq其实是因为重名才改的,随便取的名字*/,x[6]={0,0,1,-1,0,0},y[6]={1,-1,0,0,0,0},z[6]={0,0,0,0,1,-1};
char map[35][35][35];//用三维数组存储,移动方向变成了6个,z数组是移动是上下坐标的变化(上面打不下了写这里)
void f(int x)//计算秒数,这个广搜写多了都烂熟于心了
{
int sum=0;
while(pre[x])
{
sum++;
x=pre[x];
}
printf("Escaped in %d minute(s).\n",sum);
}
bool check(int x,int y,int z)//判断出界
{
if(x<r&&x>=0&&y<c&&y>=0&&z<l&&z>=0)return 1;
return 0;
}
void bfs()//广搜
{
pre[1]=0;//初始化
head=0;
tail=1;
while(head!=tail)
{
head++;
for(int i=0;i<6;i++)
{
nextx=a[head]+x[i];
nexty=b[head]+y[i];
nextz=cq[head]+z[i];//nextz是层数坐标
if(check(nextx,nexty,nextz)&&map[nextz][nextx][nexty]=='.')
{
tail++;
a[tail]=nextx;
b[tail]=nexty;
cq[tail]=nextz;
pre[tail]=head;
map[nextz][nextx][nexty]='#';//直接更改地图少用一个标记数组
if(a[tail]==getx&&b[tail]==gety&&cq[tail]==getz)//三个坐标均是目的地坐标
{
f(tail);
return ;
}
}
}
}
printf("Trapped!\n");//找到出口之后调用f();计算秒数然后退出,若没有退出(没有到达目的地)则输出此行
}
int main()
{
while(scanf("%d %d %d",&l,&r,&c)&&l!=0&&r!=0&&c!=0)
{
for(int i=0;i<l;i++)
for(int j=0;j<r;j++)
for(int k=0;k<c;k++)
{
cin>>map[i][j][k];
if(map[i][j][k]=='S')
{
a[1]=j;
b[1]=k;
cq[1]=i;
map[i][j][k]='#';
}
if(map[i][j][k]=='E')
{
getx=j;
gety=k;
getz=i;
map[i][j][k]='.';
}
}
bfs();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现