C++广度优先搜索算法之Dungeon Master

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?
你被困在3D地下城,需要找到最快的出路!地下城由单位立方体组成,可能填充或不填充岩石。移动一个单位北,南,东,西,上或下需要一分钟。您不能对角移动,迷宫四周都是坚实的岩石。
逃避可能吗?如果是,需要多长时间?

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. 
输入由许多地下城组成。每个地下城描述以包含三个整数L,R和C(均大小限制在30)的行开头。
L是构成地牢的等级数。
R和C是构成每个级别的计划的行数和列数。
然后将会有L个R行,每行包含C个字符。每个角色描述地下城的一个单元格。充满岩石的细胞用'#'表示,空单元由'。'表示。你的起始位置用'S'表示,并用字母'E'表示。每个级别后面都有一条空白行。对于L,R和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!
每个迷宫产生一行输出。如果可以到达出口,打印一行表格
      在x分钟内逃脱。
其中x被逃离所需的最短时间所取代。
如果不能逃脱,打印行
      被困!

Sample input(样例输入)


3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output(样例输出)

Escaped in 11 minute(s).
Trapped!

代码

代码如下:

#include
#include
#include
using namespace std;
int head=0,tail=1,nextx,nexty,nextz,m,n,s,beginx,beginy,beginz,endx,endy,endz;
int pre[10000000],a[10000000],b[10000000],c[10000000];
int x[6]={0,0,1,-1,0,0},y[6]={1,-1,0,0,0,0},z[6]={0,0,0,0,1,-1},total;
bool mark[200][200][200];
char map[200][200][200];
void find(int d)
{
    if(pre[d]!=0)find(pre[d]);
    total++;
}
bool check(int x,int y,int z)
{
    if(x=0&&y>=0&&z>=0)return 1;
    return 0;
}
void bfs()
{
    memset(mark,0,sizeof(mark));
    a[1]=beginx;
    b[1]=beginy;
	c[1]=beginz;
    mark[beginx][beginy][beginz]=1;
    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=c[head]+z[i];
			if(check(nextx,nexty,nextz)&&!mark[nextx][nexty][nextz]&&map[nextx][nexty][nextz]!='#')
			{
				tail++;
				a[tail]=nextx;
                b[tail]=nexty;
				c[tail]=nextz;
				pre[tail]=head;
				mark[nextx][nexty][nextz]=1; 
				if(a[tail]==endx&&b[tail]==endy&&c[tail]==endz)
				{
					find(tail);
					printf("Escaped in %d minute(s).\n",total-1);
					return ;
				}
			}
		}
	}
	printf("Trapped!\n");
}
main()
{
	while(scanf("%d%d%d",&m,&n,&s)&&m&&n&&s)
	{
		total=0;
		for(int i=0;i>map[i][j][k];
					if(map[i][j][k]=='S'){beginx=i;beginy=j;beginz=k;}
					if(map[i][j][k]=='E'){endx=i;endy=j;endz=k;}
				}
		if(beginx==endx&&beginy==endy)
        {
            printf("0\n");
            continue;
        }
		bfs();
	}
}


你可能感兴趣的:(广度优先搜索,错题大总结,搜索算法刷题集锦)