HDU 1253 胜利大逃亡

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1253

大意:能否在规定时间内从(0,0,0)——>(a-1,b-1,c-1)

#include <iostream>
#include <string>
#include <queue>
using namespace std;

int g[55][55][55];
int a, b, c;
int xx[6][3] = {{1,0,0}, {0,1,0}, {0,0,1}, {-1,0,0}, {0,-1,0}, {0,0,-1}};


struct point
{
int i, j, k, v;
};


int Bfs()
{
int i, j, k;
queue <point> q;
point q1, q2;
q1.i = q1.j = q1.k = q1.v = 0;
q.push(q1);//初始化队列


while (!q.empty())
{
q2 = q.front();//取队头
q.pop();
g[q2.i][q2.j][q2.k] = -1;



if (q2.i == a-1 && q2.j == b-1 && q2.k == c-1)//到达终点
{
return q2.v;
}
for (i = 0; i < 6; i++)//遍历相邻的点
{
q1.i = q2.i + xx[i][0];
q1.j = q2.j + xx[i][1];
q1.k = q2.k + xx[i][2];
q1.v = q2.v + 1;
if (q1.i>=0 && q1.i<a&&q1.j>=0 && q1.j<b&&q1.k>=0 && q1.k<c&&g[q1.i][q1.j][q1.k] == 0)
{//不越界且是空地
q.push(q1);
g[q1.i][q1.j][q1.k] = -1;
}
}
}
return -1;//关键。。。如果没有出口返回-1

}

int main()
{
int t;
int n, m;
int i, j, k;
int ans;
int time;
scanf("%d", &t);
while (t--)
{
scanf("%d%d%d%d", &a, &b, &c, &time);
for (i = 0; i < a; i++)
{
for (j = 0; j < b; j++)
{
for (k = 0; k < c; k++)
{
scanf("%d", &g[i][j][k]);
}
}
}

ans = Bfs();
if (ans > time)
{
puts("-1");
}
else
{
printf("%d\n", ans);
}
}
return 0;
}



你可能感兴趣的:(HDU)