[kuangbin带你飞]专题一 简单搜索- B - Dungeon Master

#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f
int l, r, c;
char s[35][35];
int mp[35][35][35];
int vis[35][35][35];
int sx, sy, sz, ex, ey, ez;
int xx[] = {-1,1,0,0,0,0};
int yy[] = {0,0,-1,1,0,0};
int zz[] = {0,0,0,0,-1,1};
int flag;
struct node
{
    int x, y, z;
    int st;
    node(int x, int y, int z, int st):x(x),y(y),z(z),st(st){};
};
int main()
{
    while(~scanf("%d %d %d", &l, &r, &c))
    {
        if(l==0&&r==0&&c==0) break;
        flag = 0;
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= l; i++)
        {
            for(int j = 1; j <= r; j++) scanf("%s", s[j]+1);
            for(int j = 1; j <= r; j++)
            {
                for(int k = 1; k <= c; k++)
                {
                    if(s[j][k]=='#'){
                        mp[j][k][i] = 1;
                    }else{
                        if(s[j][k]=='S'){
                            sx = j;
                            sy = k;
                            sz = i;
                        }else if(s[j][k]=='E'){
                            ex = j;
                            ey = k;
                            ez = i;
                        }
                    }
                }
            }
        }
        int ans;
        queue<node>q;
        node a(sx,sy,sz,0);
        q.push(a);
        vis[sx][sy][sz] = 1;
        while(!q.empty())
        {
            node p = q.front(); q.pop();
            if(p.x==ex&&p.y==ey&&p.z==ez){
                flag = 1;
                ans = p.st;
                break;
            }
            for(int i = 0; i < 6; i++)
            {
                int tx = p.x+xx[i];
                int ty = p.y+yy[i];
                int tz = p.z+zz[i];
                if(tx<1||tx>r||ty<1||ty>c||tz<1||tz>l) continue;
                if(!vis[tx][ty][tz]&&!mp[tx][ty][tz]){
                    vis[tx][ty][tz] = 1;
                    node b(tx,ty,tz,p.st+1);
                    q.push(b);
                }
            }
        }
        if(flag){
            printf("Escaped in %d minute(s).\n", ans);
        }else{
            printf("Trapped!\n");
        }
    }
    return 0;
}

你可能感兴趣的:(kuangbin)