POJ-2251 Dungeon Master (BFS)

来源:互联网 发布:mac安装完win7启动失败 编辑:程序博客网 时间:2024/06/05 08:44

题目描述
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代表一层中的行和列。S是起点,R是终点,没走一步需要一分钟,输出最小的分钟。
思路
在读入地图是先把S标记出来用begin记录他的坐标,然后再向六个方向进行广搜BFS,如果搜到地图上为E的点则结束,然后输出到该点的时间即可。
完整代码如下:

#include<stdio.h>#include<string.h>#include<math.h>#define MAX 33int l,r,c;char map[MAX][MAX][MAX];//创建地图 bool book[MAX][MAX][MAX];//对某点是否出现进行标记 const int hh[6] = {-1,1,0,0,0,0};const int xx[6] = {0,0,-1,1,0,0};const int yy[6] = {0,0,0,0,-1,1};struct node{    int x,y,h;    int s;}s[27005],begin,next;//next代表下个点 bool judge(struct node a)//判断这个点是否符合 {    if(a.x < 1 || a.y < 1 || a.h < 1 || a.x > r || a.y > c || a.h > l )        return 0;    if(map[a.h][a.x][a.y] == '#' || book[a.h][a.x][a.y] == 1)         return 0;    return 1; }int BFS(void){    int i,j,k;    int head = 1,tail = 1;    s[tail] = begin;    tail++;    while(head < tail)    {        for(i=0;i<6;i++)//向六个方向进行搜索         {            next.x = s[head].x + xx[i];            next.y = s[head].y + yy[i];            next.h = s[head].h + hh[i];            next.s = s[head].s + 1;            if((judge(next)) == 1)            {                book[next.h][next.x][next.y] = 1;                s[tail] = next;//结构体变量可以直接赋给另一个结构体变量                 tail++;            }            if(map[next.h][next.x][next.y] == 'E')                return next.s;//返回到达这个点的时间         }        head++;    }    return 0;}int main(void){    int i,j,k;    while(~scanf("%d %d %d",&l,&r,&c))    {        memset(book,0,sizeof(book));//对标记点进行初始化         if(l == 0 && r == 0 && c == 0)            break;        for(i=1;i<=l;i++)//读入整个地图         {            for(j=1;j<=r;j++)            {                for(k=1;k<=c;k++)                {                    scanf(" %c",&map[i][j][k]);//读入字符是前面加个空格可以把空白部分吸收                     if(map[i][j][k] == 'S')                    {                        book[i][j][k] = 1;                        begin.x = j;                        begin.y = k;                        begin.h = i;                        begin.s = 0;                    }                }            }        }        int ans = BFS();        if(ans)            printf("Escaped in %d minute(s).\n",ans);        else            printf("Trapped!\n");    }    return 0;}
0 0
原创粉丝点击