题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1253
题意:三维BFS,不解释
题解:DFS+剪枝会超时,裸BFS会超时,BFS+剪枝才能AC,有点伤,我以为数据会让DFS速度快一下,看来是我天真了
#include<cstdio>
#include<queue>
using namespace std;
#define FFC(i,a,b) for(int i=a;i<=b;i++)
int g[][][],a,b,c,ti,ok,ts,dir[][]={,,,,,-,,,,,-,,,,,-,,};
bool v[][][];
struct dt{int x,y,z,time;};
bool check(int x,int y,int z,int tt){
if(v[x][y][z]||g[x][y][z]||x<||x>a||y<||y>b||z<||z>c||tt>ti)return ;
else if(a+b+c-x-y-z+tt>ti)return ;//剪枝
return ;
}
void fuck(){
dt s,now;s.x=,s.y=,s.z=,s.time=;
queue<dt>Q;Q.push(s);
while(!Q.empty()){
now=Q.front();Q.pop();
if(now.x==a&&now.y==b&&now.z==c){ok=now.time;return;}
for(int i=;i<&&ok==-;i++){
int xx=now.x+dir[i][],yy=now.y+dir[i][],zz=now.z+dir[i][];
if(!check(xx,yy,zz,now.time+))continue;
s.x=xx,s.y=yy,s.z=zz,s.time=now.time+,v[xx][yy][zz]=;
Q.push(s);
}
}
}
int main(){
scanf("%d",&ts);
while(ts--){
scanf("%d%d%d%d",&a,&b,&c,&ti);ok=-;
FFC(i,,a)FFC(j,,b)FFC(k,,c){scanf("%d",&g[i][j][k]);v[i][j][k]=;}
fuck();
printf("%d\n",ok);
}
return ;
}