poj2251——Dungeon Master(BFS)

来源:互联网 发布:java volatile用法 编辑:程序博客网 时间:2024/06/07 16:09

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!

多了一个需要考虑层数的情况,但也只是多了一种入队条件罢了,没什么很难的。但我当时没想到还可以向上走就死命WA。。。。要思考得更全面啊

#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <cstdio>#include <set>#include <vector>#include <iomanip>#include <stack>#include <map>#include <queue>#define MAXN 35#define mod 2012#define INF 0x3f3f3f3fusing namespace std;int l,r,c;bool flag;char m[MAXN][MAXN][MAXN];int vis[MAXN][MAXN][MAXN];struct Node{    int x,y,z,step;};void bfs(int x,int y,int z){    Node tmp,tmp2,tmp3;    tmp.x=x;    tmp.y=y;    tmp.z=z;    tmp.step=0;    queue<Node> q;    q.push(tmp);    while(!q.empty())    {        tmp2=q.front();        q.pop();        if(m[tmp2.z][tmp2.x][tmp2.y]=='E')        {            flag=true;            cout<<"Escaped in "<<tmp2.step<<" minute(s)."<<endl;            return;        }        if(tmp2.x+1<r)        {            tmp3.x=tmp2.x+1;            tmp3.y=tmp2.y;            tmp3.z=tmp2.z;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }        if(tmp2.x-1>=0)        {            tmp3.x=tmp2.x-1;            tmp3.y=tmp2.y;            tmp3.z=tmp2.z;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }        if(tmp2.y+1<c)        {            tmp3.x=tmp2.x;            tmp3.y=tmp2.y+1;            tmp3.z=tmp2.z;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }        if(tmp2.y-1>=0)        {            tmp3.x=tmp2.x;            tmp3.y=tmp2.y-1;            tmp3.z=tmp2.z;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }        if(tmp2.z+1<l)        {            tmp3.x=tmp2.x;            tmp3.y=tmp2.y;            tmp3.z=tmp2.z+1;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }        if(tmp2.z-1>=0)        {            tmp3.x=tmp2.x;            tmp3.y=tmp2.y;            tmp3.z=tmp2.z-1;            tmp3.step=tmp2.step;            if(m[tmp3.z][tmp3.x][tmp3.y]!='#'&&!vis[tmp3.z][tmp3.x][tmp3.y])            {                vis[tmp3.z][tmp3.x][tmp3.y]=1;                tmp3.step++;                q.push(tmp3);            }        }    }}int main(){    ios::sync_with_stdio(false);    while(cin>>l>>r>>c)    {        if(l==0&&r==0&&c==0)            break;        memset(m,'#',sizeof(m));        int x,y,z;        for(int k=0; k<l; ++k)            for(int i=0; i<r; ++i)                for(int j=0; j<c; ++j)                {                    cin>>m[k][i][j];                    if(m[k][i][j]=='S')                    {                        x=i;                        y=j;                        z=k;                    }                }        memset(vis,0,sizeof(vis));        flag=false;        bfs(x,y,z);        if(!flag)            cout<<"Trapped!"<<endl;    }    return 0;}
0 0
原创粉丝点击