Dungeon Master
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 86770 | Accepted: 30496 |
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!
Source
因为把第47行的“book[nx][ny][nz]=1;”不小心写成了book[nx][ny][nz]==1; 一直时间超限。🙃
#include<string.h>
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
char g[40][40][40];
int book[40][40][40];
int d[40][40][40];
int dx[10]={0,0,1,-1,0,0};
int dy[10]={1,-1,0,0,0,0};
int dz[10]={0,0,0,0,1,-1};
int sx,sy,sz,ex,ey,ez;
int a,b,c;
struct node
{
int x,y,z;
};
void bfs()
{
memset(d,0x3f3f3f3f,sizeof(d));
memset(book,0,sizeof(book));
queue<node>q;
q.push({sx,sy,sz});
d[sx][sy][sz]=0;
book[sx][sy][sz]=1;
while(q.size())
{
node t=q.front();
q.pop();
for(int i=0;i<6;i++)
{
int nx=dx[i]+t.x;
int ny=dy[i]+t.y;
int nz=dz[i]+t.z;
if(book[nx][ny][nz]==0&&(g[nx][ny][nz]=='.'||g[nx][ny][nz]=='E')&&nx>=1&&nx<=a&&ny>=1&&ny<=b&&nz>=1&&nz<=c)
{
book[nx][ny][nz]=1;
d[nx][ny][nz]=d[t.x][t.y][t.z]+1;
if(nx==ex&&ny==ey&&nz==ez)//调整位置
{
return;
}
q.push({nx,ny,nz});
}
}
}
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c))
{
if(a==0&&b==0&&c==0)
break;
for(int i=1;i<=a;i++)
{
for(int j=1;j<=b;j++)
{
for(int k=1;k<=c;k++)
{
cin>>g[i][j][k];
if(g[i][j][k]=='S')
{
sx=i;
sy=j;
sz=k;
}
if(g[i][j][k]=='E')
{
ex=i;
ey=j;
ez=k;
}
}
}
getchar();
}
bfs();
if(d[ex][ey][ez]==0x3f3f3f3f)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",d[ex][ey][ez]);
//cout<<"Escaped in "<<d[ex][ey][ez]<<" minute(s)."<<endl;
}
return 0;
}
本文通过一个三维迷宫逃脱问题,展示了深度优先搜索(DFS)算法在解决路径寻找问题上的应用。题目中,玩家被困在一个由空格和岩石构成的3D迷宫中,目标是从起点(S)到达出口(E),只能沿着六个基本方向移动。输入包含多个迷宫,每个迷宫的维度和结构不同。程序需判断是否能到达出口并计算最短时间。提供的代码中存在一个小错误导致超时,修正后实现了正确求解路径的功能。
467

被折叠的 条评论
为什么被折叠?



