poj 2251(简单BFS)

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15359   Accepted: 5947

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 5
S....
.###.
.##..
###.#

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

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

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

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

Source

Ulm Local 1997

题目大意:一个三维的迷宫,要求走出迷宫的最少步数。如果无解,则输出Trapped!

这是我做的第二个BFS 题目,之前是想找DFS的。但是网上搜的分类竟然把这个题目分为深搜,然后我就傻逼的TLE了,当然在TLE之前我还经过无数遍的WA,甚至在编写代码的时候,连样例都过不了。。。。。后来被虐了N遍之后,才终于把它弄出来了,当然成就感也就瞬间爆表了。

解题思路:纯粹的BFS,当然别忘了,对于vis数组进行判断,否则还是会超时的。我就为此贡献了一个wa

为了纪念我的TLE,我还是贴一下我之前用DFS做的代码吧,吃一堑长一智嘛。

WA代码(DFS):

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 35
int vis[maxn][maxn][maxn];
char v[maxn][maxn][maxn];
int ans;
int l,r,c;
int bx,by,bz;
int dir[6][3]={0,-1,0,0,0,1,0,1,0,0,0,-1,1,0,0,-1,0,0};
void dfs(int z,int x,int y,int step)
{
    if(v[z][x][y]=='E')
    {
        ans=step;
        return ;
    }
    for(int i=0;i<6;i++)
    {
        int tempz=z+dir[i][0];
        int tempx=x+dir[i][1];
        int tempy=y+dir[i][2];
        if(tempz>=0&&tempz<l && tempx>=0&&tempx<r && tempy>=0&&tempy<c && !vis[tempz][tempx][tempy] && v[tempz][tempx][tempy]!='#')
        {
            z=z+dir[i][0];
            x=x+dir[i][1];
            y=y+dir[i][2];
            vis[z][x][y]=1;
            dfs(z,x,y,step+1);
            vis[z][x][y]=0;//切记做深搜的时候要记得恢复状态
            z=z-dir[i][0];
            x=x-dir[i][1];
            y=y-dir[i][2];
        }
    }
    return ;
}
int main()
{
    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                scanf("%s",v[i][j]);
                for(int k=0;k<c;k++)
                {
                    if(v[i][j][k]=='S')
                    {
                        bz=i;
                        bx=j;
                        by=k;
                    }
                }
            }
        }
        ans=0;
        memset(vis,0,sizeof(vis));
        vis[bz][bx][by]=1;//起点肯定要进行标记的
        dfs(bz,bx,by,0);
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}


下面是AC代码(BFS):

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
#ifdef __int64
typedef __int64 LL;
#else
typedef long long LL;
#endif
#define maxn 50
int bz,bx,by;
char v[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int l,r,c;
int ans;
int dir[6][3]={0,-1,0,0,0,1,0,1,0,0,0,-1,1,0,0,-1,0,0};
struct point
{
    int x,y,z;
    int step;
};
void bfs(int z,int x,int y)
{
    queue<point>p;
    point t,q;
    t.x=x; t.y=y; t.z=z; t.step=0;
    vis[t.z][t.x][t.y]=1;
    p.push(t);
    while(!p.empty())
    {
        t=p.front();p.pop();
        if(v[t.z][t.x][t.y]=='E')
        {
            ans=t.step;
            return ;
        }
        for(int i=0;i<6;i++)
        {
            q.z=t.z+dir[i][0];
            q.x=t.x+dir[i][1];
            q.y=t.y+dir[i][2];
            q.step=t.step+1;
            if(q.z>=0&&q.z<l && q.x>=0&&q.x<r && q.y>=0&&q.y<c && v[q.z][q.x][q.y]!='#' && !vis[q.z][q.x][q.y])
            {
                vis[q.z][q.x][q.y]=1;
                p.push(q);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        for(int i=0;i<l;i++)
        {
            for(int j=0;j<r;j++)
            {
                scanf("%s",v[i][j]);
                for(int k=0;k<c;k++)
                {
                    if(v[i][j][k]=='S')
                    {
                        bz=i;
                        bx=j;
                        by=k;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));
        ans=0;
        bfs(bz,bx,by);
        if(ans)
            printf("Escaped in %d minute(s).\n",ans);
        else
            printf("Trapped!\n");
    }
    return 0;
}



你可能感兴趣的:(poj 2251(简单BFS))