nyoj 353 3D dungeon
3D dungeon
时间限制:1000 ms | 内存限制:65535 KB
难度:2
- 描述
- 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!
还是感觉广搜比深搜简单点12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define MAX 35
using
namespace
std;
int
n,m,k;
int
x1,x2,y1,y2,z1,z2;
char
map[MAX][MAX][MAX];
int
vis[MAX][MAX][MAX];
struct
node
{
int
x,y,z,step;
friend
bool
operator < (node a,node b)
{
return
a.step>b.step;
}
};
void
bfs()
{
int
i,j;
int
move[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};
priority_queue<node>q;
node beg,end;
beg.x=x1;
beg.y=y1;
beg.z=z1;
beg.step=0;
q.push(beg);
vis[x1][y1][z1]=1;
while
(!q.empty())
{
end=q.top();
q.pop();
if
(end.x==x2&&end.y==y2&&end.z==z2)
{
printf
(
"Escaped in %d minute(s).\n"
,end.step);
return
;
}
for
(i=0;i<6;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
beg.z=end.z+move[i][2];
if
(!vis[beg.x][beg.y][beg.z]&&0<=beg.x&&beg.x<n&&0<=beg.y&&beg.y<m&&beg.z>=0&&beg.z<k&&map[beg.x][beg.y][beg.z]!=
'#'
)
{
map[beg.x][beg.y][beg.z]=
'#'
;
beg.step=end.step+1;
q.push(beg);
}
}
}
printf
(
"Trapped!\n"
);
}
int
main()
{
int
i,j,t,s;
while
(
scanf
(
"%d%d%d"
,&n,&m,&k)&&n!=0&&m!=0&&k!=0)
{
for
(i=0;i<n;i++)
{
for
(j=0;j<m;j++)
{
scanf
(
"%s"
,map[i][j]);
}
}
for
(i=0;i<n;i++)
{
for
(j=0;j<m;j++)
{
for
(t=0;t<k;t++)
{
if
(map[i][j][t]==
'S'
)
{
x1=i;y1=j;z1=t;
}
if
(map[i][j][t]==
'E'
)
{
x2=i;y2=j;z2=t;
}
}
}
}
memset
(vis,0,
sizeof
(vis));
bfs();
}
return
0;
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· 卧槽!C 语言宏定义原来可以玩出这些花样?高手必看!
· langchain0.3教程:从0到1打造一个智能聊天机器人