标题:三维迷宫问题
只看楼主
jing0128
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2007-8-24
 问题点数:0 回复次数:0 
三维迷宫问题

Dungeon Master
TimeLimit : 1 Second Memorylimit : 32 Megabyte
Totalsubmit : 32 Accepted : 15


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!

这是一个三维迷宫问题,我用的广度优先搜索:
我的代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct str
{
char s[31][31];
}
st[100001];
int level,r,c;
int step;
bool sign;
int dir[6][3]={{-1,0,0},{1,0,0},{0,0,-1},{0,0,1},{0,-1,0},{0,1,0}};
//方向按这个顺序:up,down,west,east,north,south
int link[100001][3];
int BFS(int lev,int x,int y)//lev是第几层,x是行,y是列,从这儿出发搜索
{
st[lev].s[x][y]='#';//已搜索的用‘#’标记
int head=0;
link[head][0]=lev;
link[head][1]=x;
link[head][2]=y;
int tail1=1,tail2=1;
sign=false;
while(head<tail1)
{
int i,j;
int lev2=link[head][0];
int x2=link[head][1];
int y2=link[head][2];
for(int i=0;i<6;i++)//六个方向搜索
{
int lev1=lev2+dir[i][0];
int x1=x2+dir[i][1];
int y1=y2+dir[i][2];
if(x1>=0&&x1<r&&y1>=0&&y1<c&&lev1>=0&&
lev1<level&&st[lev1].s[x1][y1]!='#')
{
if(st[lev1].s[x1][y1]=='E')
{
sign=true;
break;
}
else
{
link[tail2][0]=lev1;
link[tail2][1]=x1;
link[tail2][2]=y1;
tail2++;
st[lev1].s[i][j]='#';
}
}
}
head++;
if(sign)
return 1;
else
{
if(head==tail1)
{
step++;
tail1=tail2;
}
}
}
if(!sign)
return 0;
}
int main()
{
while(1)
{
scanf("%d%d%d",&level,&r,&c);
if(level==0&&r==0&&c==0)
break;
int i,j,k;
for(i=0;i<level;i++)
for(j=0;j<r;j++)
scanf("%s",st[i].s[j]);
for(i=0;i<level;i++)
for(j=0;j<r;j++)
for(k=0;k<c;k++)
if(st[i].s[j][k]=='S')
goto next;
next:
step=0;
int sub=BFS(i,j,k);
if(sub)
printf("Escaped in %d minute(s).\n",step);
else
printf("Trapped!\n");
}
system("pause");
return 0;
}
我的代码是有问题的,但是我不知道哪里出错了,请高手指点一下.

搜索更多相关主题的帖子: 三维 迷宫 dungeon unit move 
2007-08-25 11:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-165334-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.199831 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved