Miyu original, post Please note: Reprinted from __________ White House
Question address:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1253
Description:
Shengli Great Escape
Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4785 accepted submission (s): 1454
Problem descriptionignatius was taken away by the devil. One day the devil went on a business trip, which was a good opportunity for Ignatius to escape.
The Devil lives in a castle. The castle is a * B * C.CubeBody, can be expressed as a B * C matrix, just starting Ignatius is locked in (0, 0, 0) position, leaving the castle door in (A-1, B-1, C-1) now that the magic king will return to the castle in T minutes, Ignatius can go from one coordinate to one of the six adjacent coordinates every minute. now, let's show you the map of the castle. Can you calculate whether Ignatius can leave the castle before the devil returns, if the devil just came back at the exit, it would be a success). If you can, please output how many minutes to leave. If not, output-1.
The first line of input data is a positive integer k, indicating the number of test data. the first row of each group of test data is four positive integers A, B, C, and T (1 <= A, B, C <= 50,1 <= T <= 1000 ), they represent the size of the castle and the time when the devil came back. then there is a piece of input data (first 0th, then 1st, 2nd ......), each input data has B rows, and each row has c positive integers, representing the layout of the maze. 0 represents the path, and 1 represents the wall. (if you are not clear about the input description, refer to the maze description in sample input, which indicates the maze)
Note: The test data in this question is very large. Use scanf to input the data. I cannot guarantee that the use of CIN will not time out. Use Visual C ++ to submit the data on this OJ.
Output for each group of test data, if Ignatius can leave the castle before the devil returns, please output how many minutes it takes; otherwise, output-1.
Sample Input
13 3 4 200 1 1 10 0 1 10 1 1 11 1 11 0 0 0 10 1 10 0 0 00 1 1 00 1 1 0
Sample output
11
Question Analysis:
The question is a very simple BFs, but I had to worry about it all day and it was only a drop at, ym.
The difference between this question and the two-dimensional matrix is that there are six directions, between the left and right sides, and up and down. BFS can be used all the time, without any pruning ...........
CodeAs follows:
/*
Miyu original, post Please note: Reprinted from __________ White House
Http://www.cnblog.com/MiYu
Author by: miyu
Test: 1
Program: hdu1253
*/
# Include <iostream>
# Include <queue>
Using namespace STD;
Int TLE [56] [56] [56];
Const int d [6] [3] = {0, 1}, {,-1}, {0,-}, {, 0, 0 },{-, 0 }};
Int A, B, C, T, M;
Typedef struct POS {
POs () {x = y = z = n = 0 ;}
Void setpos (int A, int B, int C, int count) {x = A; y = B; Z = C; n = count ;}
Bool isend () {If (x = 1 & Y = 1 & Z = 1) return true; return false ;}
Int x, y, z;
Int N;
} Pos;
Pos t, P;
# Define CMP (A, B) (A. n <B. N)
Typedef Class Heap {
Public:
Pos H [70000*2];
Int N, P, C;
Heap (){
N = 0;
}
Void inline push (Pos e ){
For (P = ++ N; P> 1 & CMP (E, H [P> 1]); H [p] = H [P> 1], p> = 1)
;
H [p] = E;
}
Int inline pop (Pos & E ){
If (! N)
Return 0;
For (E = H [p = 1], c = 2; C <n
& CMP (H [C + = (CMP (H [C + 1], H [c]) & C <n-1)], H [N]);
H [p] = H [c], P = C, C <= 1)
;
H [p] = H [n --];
Return 1;
}
} Heap;
Heap wa;
Int Re ()
{
If (A + B + C-2> T | TLE [a] [B] [C] = 0)
Return-1;
T. setpos (a, B, c, 0 );
Tle [a] [B] [C] = 0;
Wa. Push (t );
While (WA. Pop (t )){
If (t.x + T. Y + T. Z-2> T-t.n)
Continue;
If (T. isend ())
Return T. N;
For (INT I = 0; I! = 6; ++ I ){
Int x = T. X + d [I] [0], y = T. Y + d [I] [1], Z = T. Z + d [I] [2];
If (TLE [x] [y] [Z]! = 0 ){
Tle [x] [y] [Z] = 0;
P. setpos (X, Y, Z, T. N + 1 );
If (P. isend () & P. N <= T)
Return P. N;
If (P. N <= T)
Wa. Push (P );
}
}
}
Return-1;
}
Inline bool scan_d (Int & num)
{
Char in; bool isn = false;
In = getchar ();
If (in = EOF) return false;
While (in! = '-' & (In <'0' | in> '9') in = getchar ();
If (in = '-') {isn = true; num = 0 ;}
Else num = In-'0 ';
While (in = getchar (), in> = '0' & in <= '9 '){
Num * = 10, num + = In-'0 ';
}
If (ISN) num =-num;
Return true;
}
Int main ()
{
Int K;
Scan_d (k );
While (k --){
While (WA. Pop (p ));
Memset (TLE, 0, sizeof (TLE ));
Scan_d (a); scan_d (B); scan_d (c); scan_d (t );
For (INT I = 1; I <= A; ++ I ){
For (Int J = 1; j <= B; ++ J ){
For (int K = 1; k <= C; ++ K ){
Scan_d (m );
Tle [I] [J] [k] = m = 1? 0: 1;
}
}
}
Tle [1] [1] [1] = 1;
Cout <Re () <Endl;
}
// System ("pause ");
Return 0;
}