HDU_1253 胜利大逃亡(BFS)

  纠结一晚上,注意细节啊!!!(看到有很多解题报告都用<queue>,我不太喜欢用STL的东西。。。)


My Code:

#include <iostream>
#include
<cstdio>
#include
<cstring>

using namespace std;

const int N = 51;

struct node
{
int x;
int y;
int z;
int t;
}q[N
*N*N];

int d[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, -1}, {0, 0, 1}};
int map[N][N][N];
int ans, T, X, Y, Z;

int bfs()
{
int f = 0, r = 0, i;
node p;
map[
0][0][0] = 1;
q[
0].x = 0; q[0].y = 0;
q[
0].z = 0; q[0].t = 0;
while(f <= r)
{
p
= q[f++];

for(i = 0; i < 6; i++)
{
int a = p.x + d[i][0];
int b = p.y + d[i][1];
int c = p.z + d[i][2];
if(a >= 0 && a < X && b >= 0 && b < Y && c >= 0 && c < Z && !map[a][b][c])
{
if(a == X-1 && b == Y-1 && c == Z-1 && p.t+1 <= T)
{
ans
= p.t + 1;
return 1;
}r
++;
map[a][b][c]
= 1;
q[r].x
= a;
q[r].y
= b;
q[r].z
= c;
q[r].t
= p.t+1;
}
}
}
return 0;
}

int main()
{
//freopen("data.in", "r", stdin);
int t, i, j, k;
scanf(
"%d", &t);
while(t--)
{
scanf(
"%d%d%d%d", &X, &Y, &Z, &T);

memset(map,
0, sizeof(map));

for(i = 0; i < X; i++)
for(j = 0; j < Y; j++)
for(k = 0; k < Z; k++)
scanf(
"%d", &map[i][j][k]);
ans
= 0;
if(bfs())
printf(
"%d\n", ans);
else
printf(
"-1\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/vongang/p/2149970.html