HDU 1253 胜利大逃亡

题目有个坑是可能没有到达门口的路,结果WA好几次

#include 
#include 
#include 
#include 
using namespace std;

const int INF = 10000000;
int a, b, c, d;
int s[60][60][60], cost[60][60][60];
int dx[] = {0, 0, 1, -1, 0, 0}, dy[] = {0, 0, 0, 0, 1, -1}, dz[] = {-1, 1, 0, 0, 0, 0};
struct node
{
    int a, b, c;
};
void bfs()
{
    node e, p;
    queue  que;

    for(int i = 0; i < a; i++)
        for(int j = 0; j < b; j++)
            for(int k = 0; k < c; k++)
                cost[i][j][k] = INF;

    e.a = 0, e.b = 0, e.c = 0;
    que.push(e);
    cost[0][0][0] = 0;

    while(! que.empty())
    {
        e = que.front(); que.pop();
        if(e.a == a - 1 && e.b == b - 1 && e.c == c - 1)
        {
            if(cost[e.a][e.b][e.c] <= d)
            {
                printf("%d\n", cost[e.a][e.b][e.c]);
            }
            else printf("-1\n");
            return;
        }
        for(int i = 0; i < 6; i++)
        {
            int nx = e.a + dx[i], ny = e.b + dy[i], nz = e.c + dz[i];
            if(nx >= 0 && ny >= 0 && nz >= 0 && nx < a && ny < b && nz < c && s[nx][ny][nz] == 0 && cost[nx][ny][nz] == INF)
            {
                p.a = nx, p.b = ny, p.c = nz;
                que.push(p);
                cost[nx][ny][nz] = cost[e.a][e.b][e.c] + 1;
            }
        }
    }
    printf("-1\n");
}

int main()
{
    int t;

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &a, &b, &c, &d);

        for(int i = 0; i < a; i++)
            for(int j = 0; j < b; j++)
                for(int k = 0; k < c; k++)
                    scanf("%d", &s[i][j][k]);
        bfs();
    }

	return 0;
}


你可能感兴趣的:(HDU 1253 胜利大逃亡)