hdu 1253
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<iostream> #include<queue> #include<cstring> using namespace std; int s[55][55][55]; int used[55][55][55]; int bzx[55]={1,-1,0,0,0,0}; int bzy[55]={0,0,1,-1,0,0}; int bzz[55]={0,0,0,0,1,-1}; int a,b,c,t,flag=0; struct node { int x,y,z,step;}; int bfs(int k1,int k2,int k3) { int i; queue<node>q; node s1,s2; s1.x=k1; s1.y=k2; s1.z=k3; s1.step=0; q.push(s1); used[k1][k2][k3]=1; while(!q.empty()) { s1=q.front(); if(s1.x==a-1&&s1.y==b-1&&s1.z==c-1&&s1.step<=t) return s1.step; for(i=0;i<6;i++) { s2.x=s1.x+bzx[i]; s2.y=s1.y+bzy[i]; s2.z=s1.z+bzz[i]; if(s2.x<a&&s2.x>=0&&s2.y<b&&s2.y>=0&&s2.z<c&&s2.z>=0&&0==used[s2.x][s2.y][s2.z]&&0==s[s2.x][s2.y][s2.z]) { s2.step=s1.step+1; used[s2.x][s2.y][s2.z]=1; q.push(s2); } } q.pop(); } return -1; } int main() { int m,i,j,k; cin>>m; while(m--) { cin>>a>>b>>c>>t; for(i=0;i<a;i++) for(j=0;j<b;j++) for(k=0;k<c;k++) scanf("%d",&s[i][j][k]); memset(used,0,sizeof(used)); printf("%d\n",bfs(0,0,0)); } return 0; }
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int s[55][55][55];
int used[55][55][55];
int bzx[55]={1,-1,0,0,0,0};
int bzy[55]={0,0,1,-1,0,0};
int bzz[55]={0,0,0,0,1,-1};
int a,b,c,t,flag=0;
struct node
{ int x,y,z,step;};
int bfs(int k1,int k2,int k3)
{ int i;
queue<node>q;
node s1,s2;
s1.x=k1; s1.y=k2; s1.z=k3; s1.step=0;
q.push(s1);
used[k1][k2][k3]=1;
while(!q.empty())
{ s1=q.front();
if(s1.x==a-1&&s1.y==b-1&&s1.z==c-1&&s1.step<=t)
return s1.step;
for(i=0;i<6;i++)
{ s2.x=s1.x+bzx[i]; s2.y=s1.y+bzy[i]; s2.z=s1.z+bzz[i];
if(s2.x<a&&s2.x>=0&&s2.y<b&&s2.y>=0&&s2.z<c&&s2.z>=0&&0==used[s2.x][s2.y][s2.z]&&0==s[s2.x][s2.y][s2.z])
{ s2.step=s1.step+1;
used[s2.x][s2.y][s2.z]=1;
q.push(s2);
}
}
q.pop();
}
return -1;
}
int main()
{
int m,i,j,k;
cin>>m;
while(m--)
{ cin>>a>>b>>c>>t;
for(i=0;i<a;i++)
for(j=0;j<b;j++)
for(k=0;k<c;k++)
scanf("%d",&s[i][j][k]);
memset(used,0,sizeof(used));
printf("%d\n",bfs(0,0,0));
}
return 0;
}