POJ2251 Dungeon Master(三维广搜BFS+优先队列)

来源:互联网 发布:仿瓜子二手车源码 编辑:程序博客网 时间:2024/04/20 09:57

题目:

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 28700 Accepted: 11210

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997

[Submit]   [Go Back]   [Status]   [Discuss]


代码:

这道题是三维的找最短路,S是出发点,E是终点,#是墙,.可以走,具体看注释

#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))using namespace std;char map[40][40][40];int vis[40][40][40];int go[6][3]= {{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};//六个方向int x1,y1,z1;int l,r,c;struct node{    int x,y,z,s;    friend bool operator < (node a,node b)//重装运算符,让步数从小到大排列    {        return a.s>b.s;    }};int bfs(int x,int y,int z){    node now,to;    now.x=x,now.y=y,now.z=z,now.s=0;    priority_queue<node>q;//定义优先队列    q.push(now);//当前的状态入队    vis[x][y][z]=1;//走过的标记为1    while(!q.empty())    {        now=q.top();        if(map[now.x][now.y][now.z]=='E')return now.s;//搜到以后返回步数        q.pop();        for (int i = 0; i < 6; i++)        {            int xx=now.x+go[i][0];            int yy=now.y+go[i][1];            int zz=now.z+go[i][2];            if(xx>=0&&xx<l&&yy>=0&&yy<r&&zz>=0&&zz<c&&map[xx][yy][zz]!='#'&&vis[xx][yy][zz]==0)//判断是否越界            {                to.x=xx;                to.y=yy;                to.z=zz;                to.s=now.s+1;//步数+1                vis[xx][yy][zz]=1;//标记走过的                q.push(to);//搜索下一层            }        }    }    return 0;}int main(){    while(1)    {        mem(map,0);        mem(vis,0);        scanf("%d%d%d",&l,&r,&c);        getchar();        if(l==0&&r==0&&c==0)return 0;        for(int i=0; i<l; i++)            for(int j=0; j<r; j++)            {                scanf("%s",map[i][j]);//输入三维地图                for(int k=0; k<c; k++)                {                    if(map[i][j][k]=='S')//寻找到出发点                    {                        x1=i;                        y1=j;                        z1=k;                    }                }            }        int aa=bfs(x1,y1,z1);//从出发点开始搜索        if(aa)            printf("Escaped in %d minute(s).\n",aa );        else            printf("Trapped!\n");    }    return 0;}


0 0
原创粉丝点击