poj 2251 Dungeon Master 简单队列与BFS

来源:互联网 发布:2选1数据选择器原理图 编辑:程序博客网 时间:2024/06/03 20:16
B - Dungeon Master
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status

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!

唉, 啥也不想说了,这道题做了两天了 , 一直不对, 测试数据都是错的,真的很无奈,看网上的教程发现跟他们的方法一样,而且那个广搜写的也大径相同但是我的就是不对,郁闷死我了,第二天才发现是哪里出错的,本来我定义的整个一大串的东西都是在一个结构体里面的,今天在单步调试的时候才发现如果都定义在结构体里面的话会里面的字符在传递的时候是不会更新的。我也是晕的不行。唉~且行且珍惜~~~~~~~~~~
<#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<queue>#include<iostream>#include<algorithm>using namespace std;struct node{    int x, y, z,step;}p[30000];int vmapp[31][31][31];char mapp[31][31][31];int n, m,k,r;int x,y,z,ex;int ey,ez;int to[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};int check(struct node a){    if(a.x<0 || a.x>=k || a.y>=n || a.y<0 || a.z>=m || a.z<0)        return 1;    if(vmapp[a.x][a.y][a.z])        return 1;    if(mapp[a.x][a.y][a.z]=='#')        return 1;    return 0;}int bfs(){    queue<node>q;    int i, j;    struct node a, b;    memset(vmapp,0,sizeof(vmapp));    vmapp[x][y][z]=1;    q.push(p[r]);    while(!q.empty())    {        a=q.front();        q.pop();        if(a.x==ex && a.y==ey && a.z==ez)            return a.step;        for(i=0;i<6;i++)        {            b=a;            b.x=a.x+to[i][0];            b.y=a.y+to[i][1];            b.z=b.z+to[i][2];            if(check(b))                continue;            vmapp[b.x][b.y][b.z]=1;            b.step=a.step+1;            q.push(b);        }    }    return 0;}int main(){    int i, j,e, q,ans;    while(scanf("%d%d%d",&k, &n, &m), k+n+m)    {        q=0;        for(i=0;i<k;i++)        {            for(j=0;j<n;j++)            {                for(e=0;e<m;e++)               {                   char c;                   cin>>mapp[i][j][e];                   if(mapp[i][j][e]=='S')                   {                       x=i;r=q;                       y=j;z=e;                   }                   else if(mapp[i][j][e]=='E')                   {                       ex=i;                       ey=j;ez=e;                   }                   p[q].x=i;                   p[q].y=j;                   p[q].z=e;                   p[q].step=0;                   q++;               }            }        }        ans=bfs();        if(ans)            printf("Escaped in %d minute(s).\n",ans);        else            printf("Trapped!\n");    }}
0 0
原创粉丝点击