首页 > 代码库 > hdu 1253
hdu 1253
#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;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。