HDU 1253 victory

Source: Internet
Author: User
Victory big escape time limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others) Total submission (s): 24977 accepted submission (s): 9556

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 cube of a * B * C, which can be expressed as a matrix of B * C. Ignatius was shut down at the beginning (0, 0, 0) location, exit the castle door in (A-1, B-1, C-1) location, now know that the devil 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 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0
 
Sample output
11
Code: BFS + priority queue
# Include <stdio. h ># include <queue> using namespace STD; struct node {int X, Y, Z, steps; friend bool operator <(node A, Node B) {return. steps> B. steps ;}; node start; int A, B, C, T, N, ans; int map [51] [51] [51]; int dir [6] [3] = {, 0}, {-, 0}, {0, 0}, {0,-}, {0, 0, 1 },{ 0,-1 }}; void BFS () {priority_queue <node> q; int I; node cur, next; Map [start. x] [start. y] [start. z] = 1; q. push (start); While (! Q. empty () {cur = Q. top (); // gets the first element Q. pop (); for (I = 0; I <6; I ++) // search for {next. X = cur. X + dir [I] [0]; next. y = cur. Y + dir [I] [1]; next. z = cur. Z + dir [I] [2]; If (next. * = A-1 & next. y = B-1 & next. z = C-1) // The next step is the destination {ans = cur. steps + 1; while (! Q. empty () Q. pop (); return;} If (next. x> = 0 & next. x <A & next. y> = 0 & next. Y <B & next. z> = 0 & next. z <c) if (Map [next. x] [next. y] [next. z]! = 1) {map [next. x] [next. y] [next. z] = 1; next. steps = cur. steps + 1; q. push (next) ;}}ans =-1 ;}int main () {int I, J, K, step; scanf ("% d \ n ", & N); While (n --) {scanf ("% 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", & map [I] [J] [k]); If (A + B + C-3> T) {printf ("-1 \ n"); continue;} If (Map [A-1] [b-1] [C-1] = 1) {printf ("-1 \ n"); continue;} if (a = 1 & B = 1 & C = 1) {printf ("-1 \ n"); continue;} // start when the start point and end point are the same. X = 0; start. y = 0; start. z = 0; start. steps = 0; BFS (); If (ANS> = 0 & Ans <= T) printf ("% d \ n", ANS ); else printf ("-1 \ n");} return 0 ;}

HDU 1253 victory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.