POJ2251Dungeon Master(bfs)

来源:互联网 发布:个人免费域名注册 编辑:程序博客网 时间:2024/05/17 05:50

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! 
bfs找路径,就是多了一个上楼下楼的操作,好水的题啊都不好意思写题解

AC代码

#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<algorithm>#include<cstring>using namespace std;int L,R,C;char m[35][35][35];int vis[35][35][35];int to[4][2]={0,1,1,0,0,-1,-1,0};struct node{    int x,y,step,lev;}sta,End;int ans;void bfs(node sta){    queue<node>q;    while(!q.empty())q.pop();    sta.step=0;    q.push(sta);    vis[sta.lev][sta.y][sta.x]=1;    node temp,temp1,temp2,temp3;    while(!q.empty()&&ans==-1)    {        temp=q.front();q.pop();        if(temp.lev==End.lev&&temp.x==End.x&&temp.y==End.y){ans=temp.step;break;}        for(int i=0;i<4;i++)        {            temp1.x=temp.x+to[i][0];temp1.y=temp.y+to[i][1];temp1.lev=temp.lev;temp1.step=temp.step+1;            if(!vis[temp1.lev][temp1.y][temp1.x])            {                vis[temp1.lev][temp1.y][temp1.x]=1;                if(m[temp1.lev][temp1.y][temp1.x]=='.'||m[temp1.lev][temp1.y][temp1.x]=='E')                    q.push(temp1);            }            temp2.x=temp.x;temp2.y=temp.y;temp2.lev=temp.lev+1;temp2.step=temp.step+1;            if(!vis[temp2.lev][temp2.y][temp2.x])            {                vis[temp2.lev][temp2.y][temp2.x]=1;                if(m[temp2.lev][temp2.y][temp2.x]=='.'||m[temp2.lev][temp2.y][temp2.x]=='E')                    q.push(temp2);            }            temp3.x=temp.x;temp3.y=temp.y;temp3.lev=temp.lev-1;temp3.step=temp.step+1;            if(!vis[temp3.lev][temp3.y][temp3.x])            {                vis[temp3.lev][temp3.y][temp3.x]=1;                if(m[temp3.lev][temp3.y][temp3.x]=='.'||m[temp3.lev][temp3.y][temp3.x]=='E')                    q.push(temp3);            }        }    }}void in(){    for(int i=1;i<=L;i++)        {            for(int j=1;j<=R;j++)            {                for(int k=1;k<=C;k++){                    cin>>m[i][j][k];                    if(m[i][j][k]=='S')                    {                        sta.x=k,sta.y=j;                        sta.lev=i;                    }                    else if(m[i][j][k]=='E')                    {                        End.x=k,End.y=j;                        End.lev=i;                    }                }            }        }}int main(){    while(~scanf("%d%d%d",&L,&R,&C))    {        if(L+R+C==0)break;        memset(vis,0,sizeof vis);        ans=-1;        in();        bfs(sta);        if(ans==-1)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n",ans);    }    return 0;}






原创粉丝点击