POJ 2251 Dungeon Master 简单BFS

三维的迷宫问题,常规BFS即可,代码长但写起来不难。1A。


#include <cmath>
#include<queue>
#include <cstring>
#include <cstdio>
#include <map>
#include <iostream>
#include <vector>
#include<cmath>
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
char Map[35][35][35];
int vis[35][35][35];
struct status
{
    int i,j,k,t;
};
int main()
{
    int l,r,c;
    while(cin>>l>>r>>c)
    {
        if(!l)break;
        memset(vis,0,sizeof(vis));
        status start,End;
        rep(i,1,l)
        {
            rep(j,1,r)
            {
                rep(k,1,c)
                {
                    cin>>Map[i][j][k];
                    if(Map[i][j][k]=='S')
                    {
                        start.i=i;
                        start.j=j;
                        start.k=k;
                        start.t=0;
                    }
                    if(Map[i][j][k]=='E')
                    {
                        End.i=i;
                        End.j=j;
                        End.k=k;
                        End.t=-1;
                    }
                }
            }
        }
        queue< status >que;
        que.push(start);
        vis[start.i][start.j][start.k]=1;
        int ans=-1;
        while(!que.empty())
        {
            //cout<<1<<endl;
            status now=que.front();
            que.pop();
            if(now.i==End.i&&now.j==End.j&&now.k==End.k)
            {
                ans=now.t;
                break;
                //cout<<1;
            }
            if(now.i-1>=1&&now.i-1<=l&&(Map[now.i-1][now.j][now.k]=='.'||Map[now.i-1][now.j][now.k]=='E')&&!vis[now.i-1][now.j][now.k])
            {
                status t;
                t.i=now.i-1;
                t.j=now.j;
                t.k=now.k;
                t.t=now.t+1;
                que.push(t);
                vis[now.i-1][now.j][now.k]=1;
            }
            if(now.j-1>=1&&now.j-1<=r&&(Map[now.i][now.j-1][now.k]=='.'||Map[now.i][now.j-1][now.k]=='E')&&!vis[now.i][now.j-1][now.k])
            {
                status t;
                t.i=now.i;
                t.j=now.j-1;
                t.k=now.k;
                t.t=now.t+1;
                que.push(t);
                vis[now.i][now.j-1][now.k]=1;
            }
            if(now.k-1>=1&&now.k-1<=c&&(Map[now.i][now.j][now.k-1]=='.'||Map[now.i][now.j][now.k-1]=='E')&&!vis[now.i][now.j][now.k-1])
            {

                status t;
                t.i=now.i;
                t.j=now.j;
                t.k=now.k-1;
                t.t=now.t+1;
                que.push(t);
                vis[now.i][now.j][now.k-1]=1;
            }
            if(now.k+1>=1&&now.k+1<=c&&(Map[now.i][now.j][now.k+1]=='.'||Map[now.i][now.j][now.k+1]=='E')&&!vis[now.i][now.j][now.k+1])
            {
                //cout<<1;
                status t;
                t.i=now.i;
                t.j=now.j;
                t.k=now.k+1;
                t.t=now.t+1;
                que.push(t);
                vis[now.i][now.j][now.k+1]=1;
            }
            if(now.j+1>=1&&now.j+1<=r&&(Map[now.i][now.j+1][now.k]=='.'||Map[now.i][now.j+1][now.k]=='E')&&!vis[now.i][now.j+1][now.k])
            {
                status t;
                t.i=now.i;
                t.j=now.j+1;
                t.k=now.k;
                t.t=now.t+1;
                que.push(t);
                vis[now.i][now.j+1][now.k]=1;
            }
            if(now.i+1>=1&&now.i+1<=l&&(Map[now.i+1][now.j][now.k]=='.'||Map[now.i+1][now.j][now.k]=='E')&&!vis[now.i+1][now.j][now.k])
            {
                status t;
                t.i=now.i+1;
                t.j=now.j;
                t.k=now.k;
                t.t=now.t+1;
                que.push(t);
                vis[now.i+1][now.j][now.k]=1;
            }
        }
        if(ans>0)
        {
            printf("Escaped in %d minute(s).\n",ans);
        }
        else
            cout<<"Trapped!"<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值