Dungeon Master POJ - 2251(广搜三维)

题目链接:https://cn.vjudge.net/contest/313994#problem/B
就是多加一维,其他没啥

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
int n,m,k;
int vis[33][33][33];
char a[33][33][33];
int tx[8] = {0,0,1,-1,0,0};
int ty[8] = {0,0,0,0,1,-1};
int tz[8] = {1,-1,0,0,0,0};
struct node
{
    int x;
    int y;
    int k;
    int step;
} now,after;

void BFS(int c,int s,int d)
{
    now.x = s;
    now.y = d;
    now.k = c;
    now.step = 0;
    queue<struct node> q;
    vis[now.k][now.x][now.y] = 1;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        if(a[now.k][now.x][now.y]=='E')
        {
            cout<<"Escaped in ";cout<<now.step;cout<<" minute(s)."<<endl;
            return;
        }
        for(int i=0; i<6; i++)
        {
            after.x = now.x + tx[i];
            after.y = now.y + ty[i];
            after.k = now.k + tz[i];
            if(after.k>=0&&after.k<k&&after.x>=0&&after.x<n&&after.y>=0&&after.y<m)
            {
                if(!vis[after.k][after.x][after.y]&&a[after.k][after.x][after.y]!='#')
                {
                    after.step = now.step + 1;
                    vis[after.k][after.x][after.y] = 1;
                    q.push(after);
                }
            }
        }
    }
    cout<<"Trapped!"<<endl;
}


int main()
{
    ios::sync_with_stdio(false);
    while(cin>>k>>n>>m&&n&&m&&k)
    {
        memset(vis,0,sizeof(vis));
        int x,y,z,i,j;
        for(i=0; i<k; i++)
        {
            for(j=0; j<n; j++)
            {
                cin>>a[i][j];
                for(int p=0; p<m; p++)
                {
                    if(a[i][j][p]=='S')
                    {
                        z = i;
                        x = j;
                        y = p;
                    }
                }
            }
        }
        BFS(z,x,y);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值