HDU 胜利大逃亡

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define CL(x, y) memset(x,y,sizeof(x))
using namespace std;
const int MAX = 55;
int N, T, A, B, C, i, j, k;
int used[MAX][MAX][MAX], tower[MAX][MAX][MAX];
int Move[6][3]= {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
void BFS(int a, int b, int c);
bool checked(int x, int y, int z);
struct node
{
    int x, y, z;
    int time;
};
queue <node> Q;
int main()
{
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d%d%d%d",&A,&B,&C,&T);
        for(i = 0; i < A; i++)
            for(j = 0; j < B; j++)
                for(k = 0; k < C; k++)
                    scanf("%d",&tower[i][j][k]);
        if(tower[A-1][B-1][C-1])//网上的,出口是墙
        {
            printf("-1
");
            continue;
        }
        if(A+B+C-3 > T)//最短路径
        {
            printf("-1
");
            continue;
        }
        if(A==1 && B==1 && C==1)//考虑起点和终点相同的情况
        {
            printf("0
");
            continue;
        }
        if(tower[A-2][B-1][C-1] && tower[A-1][B-2][C-1] && tower[A-1][B-1][C-2])//至少有一个可以到出口
        {
            printf("-1
");
            continue;
        }
        CL(used, 0);
        BFS(0, 0, 0);
    }
    return 0;
}
void BFS(int a, int b, int c)//(a,b,c)代表所在坐标
{
    int xx, yy, zz;
    while(!Q.empty())
        Q.pop();
    node first, cur, next;
    first.x = 0;
    first.y = 0;
    first.z = 0;
    first.time = 0;
    used[a][b][c] = 1;
    Q.push(first);
    while(!Q.empty())
    {
        cur = Q.front();
        Q.pop();
        if(A-cur.x+B-cur.y+C-cur.z-3 > T)//最短路径
        {
//            printf("-1
");
            continue;
        }
        if(cur.time > T)
            printf("-1
");
        if(cur.x==A-1 && cur.y==B-1 && cur.z==C-1)
        {
            printf("%d
", cur.time);
            return ;
        }
        for(i = 0; i < 6; i++)
        {
            xx = cur.x+Move[i][0];
            yy = cur.y+Move[i][2];
            zz = cur.z+Move[i][1];
            if(checked(xx, yy, zz) && !used[xx][yy][zz] && !tower[xx][yy][zz])//哥哥,0代表路,请看清题目!tower[xx][yy][zz]
            {
                next.x = xx;
                next.y = yy;
                next.z = zz;
                next.time = cur.time+1;
                used[xx][yy][zz] = 1;
                if(A-next.x+B-next.y+C-next.z-3 > T)//剪枝
                    continue;
                Q.push(next);
            }
        }
    }
    printf("-1
");
    return ;
}
bool checked(int x, int y, int z)
{
    if(x>=0 && x<A && y>=0 && y<B && z>=0 && z<C)
        return true;
    return false;
}
View Code

这个是数据的问题,尝试一下C++的测试

原文地址:https://www.cnblogs.com/ghostTao/p/4326265.html