The Great Escape of victory
Time limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 29528 Accepted Submission (s): 11136
Problem Description
Ignatius was taken by the Devil, one day the devil on business trip, this is Ignatius escape good chance.
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 was locked in (0,0,0) position, leaving the castle door in (a-1,b-1,c-1) position, now know that the devil will be in T minutes back to the castle, Ignatius can walk from one coordinate to one of the six adjacent coordinates per minute. Now give you the map of the castle, please calculate the Ignatius can leave the castle before the Devil return (as long as the exit to leave the castle, if go to the exit when the devil just came back also counted escape success), If you can please output how many minutes to leave, if not then 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. (If the input description is not clear, you can refer to the Maze description in sample input, which represents the maze in)
Special note: The test data is very large, please use scanf input, I can not guarantee that using CIN can not time out. On this OJ, submit using Visual C + +.
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
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
Sample Output
11
#include <iostream> #include <stdio.h> #include <memory.h> #include <queue> #include < algorithm> #include <string.h> #include <stdio.h> #define N -Using namespace Std;struct node{intX, Y, ZintTime;};intdirections[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};intA,b,c,t;intMap[n][n][n];node My,cur,nextt;//Use next to submit the CE, can not use next reason is that next may be other meaningintBFS (intXintYintZ) {queue<node>q;The /**queue class is a container adapter that provides a queue of functionality for programmers, specifically, a FIFO (first-in, first-out) data structure **/My.x=x; My.y=y; My.z=z; My.time=0; Q.push (my);/**push () will place an element into the queue. **/ while(!q.empty ())/**empty () determines that the queue is empty and returns True when the queue is empty. **/{Cur=q.front ();/**front () returns the first element within the queue (that is, the first element to be placed). **/Q.pop ();/**pop () removes an element from a queue. Note: Pop () removes the next element, but does not return it, front () and back () return the next element without removing the element. **/ for(intI=0;i<6; i++) {nextt.x=cur.x+directions[i][0]; nextt.y=cur.y+directions[i][1]; nextt.z=cur.z+directions[i][2];if(nextt.x<a&&nextt.y<b&&nextt.z<c&&nextt.x>=0&&nextt.y>=0&&nextt.z>=0&&map[nextt.x][nextt.y][nextt.z]!=1&&nextt.time<=t) {if(nextt.x==a-1&&nextt.y==b-1&&nextt.z==c-1)/** If you find an exit, return the number of steps that are currently taking **/ returncur.time+1; nextt.time=cur.time+1;/** The next step can go that direction, the direction of information perfect after entering the queue **/Q.push (NEXTT);/**push () will place an element into the queue. **/map[nextt.x][nextt.y][nextt.z]=1;/** Flag The node in that direction has passed **/} } }return-1;}intMain () {intSum,i,j,x,k; scanf"%d", &x); while(x--) {scanf ("%d%d%d%d", &a,&b,&c,&t); for(i=0; i<a;i++) { for(k=0; k<b;k++) { for(j=0; j<c;j++) {scanf ("%d", &map[i][k][j]); }}} SUM=BFS (0,0,0); printf"%d\n", sum); }return 0;}
Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link, thank you.
HDOJ1253 Victory Big Escape BFS