POJ-Dungeon Master

来源:互联网 发布:淘宝要好评话术 编辑:程序博客网 时间:2024/05/02 21:03
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36715 Accepted: 14013

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

原题链接

Hint:

求最短路径时优先考虑BFS而不是DFS

#include <iostream> ///Memort:1124kb  Length:1968  Lang:G++#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>using namespace std;char Map[44][44][44];int v[44][44][44];int dd[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};int l,r,c;int s1,s2,s3,d1,d2,d3;struct node{    int x,y,z;    int step;};int judge(int x,int y,int z){    if(x<0 || y<0 || z<0 || x>=l || y>=r || z>=c)        return 1;    else if(Map[x][y][z] == '#')        return 1;    else if(v[x][y][z])        return 1;    return 0;}int BFS(){    int i;    node q1,q2;    queue<node>q;    v[s1][s2][s3]=1;    q1.x=s1,q1.y=s2,q1.z=s3;    q1.step=0;    q.push(q1);    while(!q.empty())    {        q1=q.front();        q.pop();        if(q1.x==d1&&q1.y==d2&&q1.z==d3)        {            return q1.step;        }        for(i=0; i<6; i++)        {            q2.x=q1.x+dd[i][0];            q2.y=q1.y+dd[i][1];            q2.z=q1.z+dd[i][2];            if(judge(q1.x+dd[i][0],q1.y+dd[i][1],q1.z+dd[i][2])==1)            {                continue;            }            else                v[q2.x][q2.y][q2.z]=1;            q2.step=q1.step+1;            q.push(q2);        }    }    return 0;}int main(){    int i,j,k;    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)    {        memset(v,0,sizeof(v));        for(i=0; i<l; i++)        {            for(j=0; j<r; j++)            {                scanf("%s",Map[i][j]);                for(k=0; k<c; k++)                {                    if(Map[i][j][k]=='S')                    {                        s1=i,s2=j,s3=k;                    }                    if(Map[i][j][k]=='E')                    {                        d1=i,d2=j,d3=k;                    }                }            }        }        int ans=BFS();        if(ans)            printf("Escaped in %d minute(s).\n", ans);        else            printf("Trapped!\n");    }    return 0;}

///错误代码
#include <bits/stdc++.h>using namespace std;char Map[33][33][33];int v[33][33][33];int s1,s2,s3,d1,d2,d3,l,r,c;int dd[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}};struct node{    int x,y,z;    int step;}q1,q2;int judge(int x,int y,int z)///错因judge函数的判断条件错了,if语句中的判断条件应为“或”关系{    if(x>=0&&x<=l-1&&y>=0&&y<=r-1&&z>=0&&z<=c-1&&!v[x][y][z]&&Map[x][y][z]=='.')        return 1;    else        return 0;}int BFS(){    int i;    queue<node>q;    q1.x=s1,q1.y=s2,q1.z=s3;    q1.step=0;    v[s1][s2][s3]=1;    q.push(q1);    while(!q.empty())    {       q1=q.front();       q.pop();       if(q1.x==d1&&q1.y==d2&&q1.z==d3)        return q1.step;       for(i=0;i<6;i++)       {           q2.x=q1.x+dd[i][0];           q2.y=q1.y+dd[i][1];           q2.z=q1.z+dd[i][2];           if(judge(q2.x,q2.y,q2.z)==1)           {               v[q2.x][q2.y][q2.z]=1;               q2.step=q1.step+1;               q.push(q2);           }           else            continue;       }    }    return 0;}int main(){    int i,j,k,ans;    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)    {        memset(v,0,sizeof(v));        for(i=0; i<l; i++)        {            for(j=0; j<r; j++)            {                scanf("%s",Map[i][j]);                for(k=0; k<c; k++)                {                    if(Map[i][j][k]=='S')                    {                        s1=i,s2=j,s3=k;                    }                    if(Map[i][j][k]=='E')                    {                        d1=i,d2=j,d3=k;                    }                }            }        }        ans=BFS();        if(ans)            printf("Escaped in %d minute(s).\n",ans);        else            printf("Trapped!\n");    }    return 0;}


原创粉丝点击