http://acm.hdu.edu.cn/showproblem.php?pid=1253
题意:
给你个三维空间,问在规定的时间内能否从起点走到终点。思路:
很水的BFS啊啊啊,我挂在起始位置。。。起点不用考虑啊啊啊。。。 各种蛋疼有木有。。。
我的代码:
/*
program:hdu_1253
author:BlackAndWhite
*/
#include<stdio.h>
int a[51][51][51],ni,nj,nz,K,T;
int i,j,z,dir[6][3]={1,0,0, -1,0,0, 0,1,0, 0,-1,0,0,0,1, 0,0,-1};
struct node
{
int x,y,z;
}que[1000001];
int bfs()
{
int push=1,pop=0;
struct node t,q;
t.x=0;t.y=0;t.z=0;
if(a[nz-1][ni-1][nj-1]) return 0;//起点不用考虑啊,我勒个XX
a[t.z][t.x][t.y]=1;que[0]=t;
while(pop<push)
{
t=que[pop++];
if(a[t.z][t.x][t.y]>T+1){return 0;}
if(t.z==nz-1&&t.x==ni-1&&t.y==nj-1) return 1;
for(i=0;i<6;i++)
if(t.z+dir[i][0]<nz&&t.z+dir[i][0]>=0
&&t.x+dir[i][1]<ni&&t.x+dir[i][1]>=0
&&t.y+dir[i][2]<nj&&t.y+dir[i][2]>=0
&&!a[t.z+dir[i][0]][t.x+dir[i][1]][t.y+dir[i][2]])
{
a[t.z+dir[i][0]][t.x+dir[i][1]][t.y+dir[i][2]]=a[t.z][t.x][t.y]+1;
q.z=t.z+dir[i][0];
q.x=t.x+dir[i][1];
q.y=t.y+dir[i][2];
que[push++]=q;
}
}
return 0;
}
int main()
{
scanf("%d",&K);
while(K--)
{
scanf("%d%d%d%d",&nz,&ni,&nj,&T);
for(z=0;z<nz;z++)
for(i=0;i<ni;i++)
for(j=0;j<nj;j++)
scanf("%d",&a[z][i][j]);
if(bfs()) printf("%d\n",a[nz-1][ni-1][nj-1]-1);
else printf("-1\n");
}
return 0;
}
/*
我勒个去,一水题淹死我了
12
3 3 4 10
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
*/