Topic Link: HDU 1253, the main idea: walk from the lower left corner of a cube to the upper right corner (the line between two points for the box of the body diagonal) the minimum time (steps), not to go or the minimum time does not reach the required output-1, otherwise output the time.
Because the requirements of the "least" and so on, so it is easy to think of the broad search, yes, this is the BFS of the naked problem, but its state number is a three-dimensional array, the state of transfer (direction) from the two-dimensional array of 4 has become 6, but the principle is the same, direct wide search can, but this problem is WA took a few rounds to see the discussion area only to know the original starting point whether it is not the wall is not tube (continue search), just look at the end.
First, the first version of the code, with a three-dimensional DP array to record from the starting point to the current node time/step number, clear:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <queue>5 using namespacestd;6 #definefor (i,s,t) for (int i=s; i<t; ++i)7 Const intinf=0x2fffffff;8 inta,b,c,t;9 intf[ -][ -][ -], dp[ -][ -][ -];Ten One intdir[8][3]= {{0,0,1},{0,1,0},{-1,0,0},{1,0,0},{0,-1,0},{0,0,-1}}; A - structnode{ - intx, y, z theNodeintx=0,inty=0,intz=0): X (x), Y (y), Z (z) {} - }; - - voidsolve () { +scanf"%d%d%d%d",&a,&b,&c,&T); -for (I,0, A +2) for (J,0, B +2) for (K,0, c+2) { +f[i][j][k]=1; dp[i][j][k]=inf; A } atfor (I,1, A +1) for (J,1, B +1) for (K,1, c+1) -scanf"%d", f[i][j]+k); - - //If the end of the wall will never get out, (this problem is very pit, starting point whether it is not the wall does not matter) - //or from the beginning to the end of the shortest straight line distance than the big words also can not out, is a very important pruning - if(f[a][b][c]==1|| a+b+c-3> T) {puts ("-1");return ; } in -Queue<node>Q; toQ.push (Node (1,1,1)); +dp[1][1][1]=0; - while(Q.size ()) { theNode p=Q.front (); Q.pop (); * for(intk=0; k<6; ++k) { $ intdx= p.x+dir[k][0], dy= p.y+dir[k][1], dz= p.z+dir[k][2];Panax Notoginseng if(f[dx][dy][dz]!=1&& dp[dx][dy][dz]==inf) { -dp[dx][dy][dz]= dp[p.x][p.y][p.z]+1; the Q.push (Node (DX,DY,DZ)); + } A } the } + //after you have reached the end of time, you also need to decide whether to meet test instructions - if(Dp[a][b][c] > T) puts ("-1"); $ Elseprintf"%d\n", Dp[a][b][c]); $ } - - intMain () { the intK; -scanf"%d",&k);Wuyi while(k--) solve (); the return 0; -}
View Code
And then the second version, the DP array is omitted, and a variable is added to the structure of the node to record the time:
1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <queue>5#include <algorithm>6 using namespacestd;7 #definefor (i,s,t) for (int i=s; i<t; ++i)8 Const intinf=0x2fffffff;9 inta,b,c,t;Ten intf[ -][ -][ -]; One A intdir[8][3]= {{0,0,1},{0,1,0},{-1,0,0},{1,0,0},{0,-1,0},{0,0,-1}}; - - structnode{ the intx,y,z,t; -Nodeintx=0,inty=0,intz=0,intt=inf): X (x), Y (y), Z (z), T (t) {} - BOOL operator==(ConstNode &n2)Const { - returnx==n2.x && y==n2.y && z==n2.z; + } - }; + A voidsolve () { atscanf"%d%d%d%d",&a,&b,&c,&T); -for (I,0, A +2) for (J,0, B +2) for (K,0, c+2) -f[i][j][k]=1; -for (I,1, A +1) for (J,1, B +1) for (K,1, c+1) -scanf"%d", f[i][j]+k); - in //If the end of the wall will never get out, (this problem is very pit, starting point whether it is not the wall does not matter) - //or from the beginning to the end of the shortest straight line distance than the big words also can not out, is a very important pruning to if(f[a][b][c]==1|| a+b+c-3> T) {puts ("-1");return ; } + - node Ed (a,b,c); theQueue<node>Q; *Q.push (Node (1,1,1,0)); $ while(Q.size ()) {Panax NotoginsengNode p=Q.front (); Q.pop (); - if(p = = ed) ed.t=min (ed.t,p.t); the for(intk=0; k<6; ++k) { + intdx= p.x+dir[k][0], dy= p.y+dir[k][1], dz= p.z+dir[k][2]; A if(f[dx][dy][dz]!=1){ theQ.push (Node (dx,dy,dz,p.t+1)); +f[dx][dy][dz]=1; - } $ } $ } - //after you have reached the end of time, you also need to decide whether to meet test instructions - if(ed.t > T) puts ("-1"); the Elseprintf"%d\n", ed.t); - }Wuyi the intMain () { - intK; Wuscanf"%d",&k); - while(k--) solve (); About return 0; $}
View Code
Direction Array dir[][][] Sure enough, the X, Y, Z package is a virial better.
HDU 1253 Victory Grand Escape