hdu 1253 勝利大逃亡
來源:程序員人生 發(fā)布時間:2015-04-17 09:14:06 閱讀次數(shù):2881次
簡單空間bfs,開3維數(shù)組剪枝1026ms過
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int a,b,c,t;
int re;
int mapp[51][51][51];
int dir[6][3]={{0,0,1},{0,0,⑴},{1,0,0},{⑴,0,0},{0,1,0},{0,⑴,0}};
struct stu
{
int x,y,z,s;
};
void bfs()
{
stu x,y;
queue<stu>root;
x.x=0;x.y=0;x.z=0;x.s=0;
root.push(x);
mapp[x.x][x.y][x.z]=1;
while(root.size())
{
x=root.front();
root.pop();
if(x.x==a⑴&&x.y==b⑴&&x.z==c⑴)
{
re=x.s;return;
}
for(int i=0;i<6;i++)
{
y.x=x.x+dir[i][0];
y.y=x.y+dir[i][1];
y.z=x.z+dir[i][2];
y.s=x.s+1;
if(y.x<0||y.x>=a||y.y<0||y.y>=b||y.z<0||y.z>=c||mapp[y.x][y.y][y.z]){continue;}
if(y.s<t)
{
root.push(y);
mapp[y.x][y.y][y.z]=1;
}
}
}
}
int main()
{
int p;
scanf("%d",&p);
while(p--)
{
scanf("%d%d%d%d",&a,&b,&c,&t);
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
for(int k=0;k<c;k++)
{
scanf("%d",&mapp[i][j][k]);
}
}
}
if(a+b+c⑶>t||mapp[a⑴][b⑴][c⑴])
{
printf("⑴
");
continue;
}
if(a==1&&b==1&&c==1)
{
printf("0
");
continue;
}
re=⑴;
bfs();
printf("%d
",re);
}
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈