The Great Escape of victoryTime
limit:4000/2000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 28472 Accepted Submission (s): 10782
Problem Descriptionignatius was captured by the devil, one day the devil on Business, 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.
The first line of 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 please export the minimum number of minutes he needs, 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 The subject is in 6 directions: up, down, left, right, before, after. Creates a three-dimensional array that records the current position state.#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std;# Define N 60int map[n][n][n];int vis[n][n][n];int t,a,b,c,tt;int dx[] = {0,0,-1,1,0,0};int dy[] = {0,0,0,0,-1,1};int dz[] = { -1,1,0,0,0,0};struct point{int x,y,z,step;}; int BFs () {if (A-1 = = 0 && b-1 = 0 && c-1 = 0)//If the exit position is the same as the inlet, return directly to 0.return 0;point temp, head, Tt;que Ue<point>q;while (!q.empty ()) Q.pop (); vis[0][0][0] = 1;temp.x = 0; TEMP.Y = 0; temp.z = 0; Temp.step = 0;q.push (temp), while (!q.empty ()) {head = Q.front (), Q.pop (); for (int i = 0; i < 6; i++) {tt.x = head.x + dx[i ];tt.y = head.y + dy[i];tt.z = head.z + dz[i];if (Tt.x < 0 | | Tt.y < 0 | | Tt.z < 0 | | tt.x >= A | | tt.y >= B | | Tt.z >= C | | vis[tt.x][tt.y][tt.z]| | MAP[TT.X][TT.Y][TT.Z]) Continue;tt.step = Head.step + 1;vis[tt.x][tt.y][tt.z] = 1;if (tt.x = A-1 && Tt.y = = B- 1 && tt.z = = c-1) return Tt.step;q.push (TT);}} Return (-1);//returns-1 if no exit is found. int main (){scanf ("%d", &t), while (t--) {memset (Vis, 0, sizeof (VIS)), scanf ("%d%d%d%d", &a, &b, &c, &TT); int i = 0; i < A; i++) {for (int j = 0; J < b; j + +) {for (int k = 0; k < C; k++) {scanf ("%d", &map[i][j][k]);}} int ans = BFS (); if (ans<0 | | ans>tt)//return value less than 0 cannot find exit, greater than the devil's time can not escape. printf (" -1\n"); elseprintf ("%d\n", ans);} return 0;}
HDU----1253 Victory Grand Escape BFS