#include <iostream>#include<cstdio>#include<cstring>#include<queue>#defineCL (x, y) memset (x,y,sizeof (x))using namespacestd;Const intMAX = -;intN, T, A, B, C, I, J, K;intUsed[max][max][max], Tower[max][max][max];intmove[6][3]= {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};voidBFS (intAintBintc);BOOL checked(intXintYintz);structnode{intx, y, Z; intTime ;}; Queue<node>Q;intMain () {scanf ("%d",&N); while(n--) {scanf ("%d%d%d%d",&a,&b,&c,&T); for(i =0; i < A; i++) for(j =0; J < B; J + +) for(k =0; K < C; k++) scanf ("%d",&Tower[i][j][k]); if(tower[a-1][b-1][c-1])//on the Internet, the exit is the wall{printf ("-1\n"); Continue; } if(a+b+c-3> T)//Shortest Path{printf ("-1\n"); Continue; } if(a==1&& b==1&& c==1)//consider the same starting point and end Point{printf ("0\n"); Continue; } if(tower[a-2][b-1][c-1] && tower[a-1][b-2][c-1] && tower[a-1][b-1][c-2])//at least one can go to the exit{printf ("-1\n"); Continue; } CL (Used,0); BFS (0,0,0); } return 0;}voidBFS (intAintBintC//(a,b,c) represents the coordinates of the location{ intxx, yy, ZZ; while(!Q.empty ()) Q.pop (); Node first, cur, next; First.x=0; First.y=0; First.z=0; First.time=0; USED[A][B][C]=1; Q.push (first); while(!Q.empty ()) {cur=Q.front (); Q.pop (); if(a-cur.x+b-cur.y+c-cur.z-3> T)//Shortest Path {//printf (" -1\n"); Continue; } if(Cur.time >T) printf ("-1\n"); if(cur.x==a-1&& cur.y==b-1&& cur.z==c-1) {printf ("%d\n", Cur.time); return ; } for(i =0; I <6; i++) {xx= cur.x+move[i][0]; YY= cur.y+move[i][2]; ZZ= cur.z+move[i][1]; if(checked(xx, yy, ZZ) &&!used[xx][yy][zz] &&!tower[xx][yy][zz])//Brother, 0 represents the road, please see the topic!tower[xx][yy][zz]{next.x=xx; Next.y=yy; Next.z=ZZ; Next.time= cur.time+1; USED[XX][YY][ZZ]=1; if(a-next.x+b-next.y+c-next.z-3> T)//Pruning Continue; Q.push (next); }}} printf ("-1\n"); return ;}BOOL checked(intXintYintz) { if(x>=0&& x<a && y>=0&& y<b && z>=0&& z<C)return true; return false;}View Code
This is a problem with the data, try the C + + test
HDU Victory Grand Escape _bfs