B

题目大意:
地牢大师(感觉像是一款游戏啊.......)
    你被困在一个3D的地牢里面,并且需要发现最快的出去的路,这个地牢由很多小立方体组成,有的是空的可以走,有的被岩石填充了不可以走,移动一下要花费1分钟的时间(可以向前后左右上下移动),不能对角移动和移动到迷宫外面,因为迷宫四周都是有岩石包围的。
这是一个逃亡的问题,你需要花费多长时间呢?
//////////////////////////////////////////////////////
简直就是广搜的模板题......直接上吧,1A不错不错
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

#define maxn 100

struct node
{
    int x, y, z, step;
};

int h, m, n;//高,长和宽
//6个方向可以走
int dir[6][3] = { {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };
char G[maxn][maxn][maxn];

int OK(int z, int x, int y)//判断这点是否可以走
{
    if(z>=0&&z<h && x>=0&&x<m && y>=0&&y<n && G[z][x][y] != '#')
        return 1;
    return 0;
}
int DFS(node s, node e)
{
    queue<node> Q;
    Q.push(s);

    while(Q.size())
    {
        s = Q.front();Q.pop();

        if(s.x==e.x&&s.y==e.y&&s.z==e.z)
            return s.step;

        for(int i=0; i<6; i++)
        {
            node q = s;
            q.x += dir[i][0];
            q.y += dir[i][1];
            q.z += dir[i][2];
            q.step += 1;

            if(OK(q.z, q.x, q.y) == 1)
            {
                G[q.z][q.x][q.y] = '#';
                Q.push(q);
            }
        }
    }

    return -1;
}

int main()
{
    while(scanf("%d%d%d", &h, &m, &n), h+m+n)
    {
        int i, j, k;
        node s, e;

        for(i=0; i<h; i++)
        for(j=0; j<m; j++)
        {
            scanf("%s", G[i][j]);
            for(k=0; k<n; k++)
            {
                if(G[i][j][k] == 'S')
                {
                    s.z = i;
                    s.x = j;
                    s.y = k;
                    s.step = 0;
                }
                if(G[i][j][k] == 'E')
                {
                    e.z = i;
                    e.x = j;
                    e.y = k;
                }
            }
        }


        int ans = DFS(s, e);

        if(ans != -1)
            printf("Escaped in %d minute(s). ", ans);
        else
            printf("Trapped! ");
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4647851.html