Nine degrees OJ topic 1456: Victory Grand Escape

Source: Internet
Author: User

Title Description:

Ignatius was taken by the Devil, one day the devil on business trip, this is a good chance of Ignatius escape. The Devil lives in a castle, the castle is a a*b*c cube, can be expressed as a b*c matrix, just beginning Ignatius is locked in (0,0,0) position, Leaving the gate of the castle in the position of (a-1,b-1,c-1), now know that the devil will return to the castle in T minutes, and Ignatius can walk from one coordinate to one of the neighboring six coordinates per minute. Now give you a map of the castle, please calculate whether Ignatius can leave the castle before the Devil Returns ( As long as the exit, even if you leave the castle, if you go to the exit when the devil just came back to be counted as escape success), if you can please output how many minutes to leave, if not the output-1.

Input:

The first line of the input data is a positive integer k, indicating the number of test data. The first line of the test data for each group 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 of the Devil's return. Then a block of input data (first NO. 0 block, then 1th block, 2nd block ...), each input data has B line, each line has a C positive integer, representing the layout of the maze, of which 0 represents the road, 1 represents the wall.

Output:

For each set of test data, if Ignatius can leave the castle before the Devil returns, then output-1 if the minimum number of minutes is required.

Sample input:
 
  
Sample output:
11

Three-dimensional maze
Search by Breadth First
The code is as follows
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <queue>5#include <iostream>6 using namespacestd;7 8 intdir[][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};9 intcubic[ the][ the][ the];Ten inttcnt[ the][ the][ the]; One  A structpoint{ -     intx, y, Z, t; -Point (intXintYintZintt) { the          This->x = x, This->y = y, This->z = Z, This->t =T; -     } - }; -  +Queue <Point>que; -  + intMainintargcChar Const*argv[]) A { at     inta,b,c,t; -     //freopen ("Input.txt", "R", stdin); -     intK; -      while(SCANF ("%d", &k)! =EOF) { -          while(k--) { -scanf"%d %d%d%d",&a,&b,&c,&t); in              for(inti =0; i < A; i++) { -                  for(intj =0; J < b; J + +) { to                      for(intK =0; K < C; k++) { +scanf"%d",&cubic[i][j][k]); -                     } the                 } *             } $              while(!Que.empty ()) {Panax Notoginseng Que.pop (); -             } thememset (tcnt,-1,sizeof(tcnt)); +Point O (0,0,0,0); A Que.push (O); the             intAns =-1; +              while(!Que.empty ()) { -Point P =Que.front (); Que.pop (); $                 intT0 =p.t; $                 if(T0 >t) { -                     Continue; -                 } the                 if(p.x = =-1&& P.Y = = b1&& P.z = = C1) { -Ans =t0;Wuyi                      Break; the                 } -                  for(inti =0; I <6; i++) { Wu                     intXT = p.x + dir[i][0]; -                     intYT = p.y + dir[i][1]; About                     intZT = p.z + dir[i][2]; $                     inttt = t0 +1; -                     if(XT < a && XT >=0&& YT < b && YT >=0&& ZT < c && ZT >=0&& Cubic[xt][yt][zt] = =0&& Tcnt[xt][yt][zt] = =-1) { - Point Toadd (XT,YT,ZT,TT); - Que.push (toadd); ATCNT[XT][YT][ZT] =tt; +                     } the                 } -             } $printf"%d\n", ans);  the         } the     } the         return 0; the}

Nine degrees OJ topic 1456: Victory Grand Escape

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.