三维bfs(HUD1253胜利大逃亡)

#include <stdio.h>
#include <string.h>
int map[51][51][51];
int v[51][51][51];
int a,b,c,t11;
struct node
{
int x,y,z,ans;
}q[200001];
int jx[6]={0,0,0,0,-1,1};
int jy[6]={0,0,1,-1,0,0};
int jz[6]={1,-1,0,0,0,0};
void bfs()
{
memset(v,0,sizeof(v));
struct node t,f;
int e=0;
int s=0;
t.x=0;
t.y=0;
t.z=0;
v[0][0][0]=1;
t.ans=0;
q[e++]=t;
while(s<e)
{
t=q[s++];
if((t.x==a-1)&&(t.y==b-1)&&(t.z==c-1)&&(t.ans<=t11))
{
printf("%d ",t.ans);
return ;
}
for(int i=0;i<6;i++)
{
f.x=t.x+jx[i];
f.y=t.y+jy[i];
f.z=t.z+jz[i];
if(f.x>=0&&f.x<a&&f.y>=0&&f.y<b&&f.z>=0&&f.z<c&&v[f.x][f.y][f.z]==0&&map[f.x][f.y][f.z]==0)
{
f.ans=t.ans+1;//我二逼似的在这里写了f.ans=t.ans++;那么f.ans=0;
q[e++]=f;
v[f.x][f.y][f.z]=1;
}
}
}
printf("-1 ");
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&t11);
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
for(int k=0;k<c;k++)
scanf("%d",&map[i][j][k]);

}
bfs();

}
return 0;
}

原文地址:https://www.cnblogs.com/zhangmingcheng/p/3784619.html