Nightmare Time
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U Submit Status Practice HDU 1072
Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The Labyrinth has a exit, Ignatius should get out of the the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, which is to move from one area to the nearest are A (that's, if Ignatius stands on (x, y) Now, he could only on (X+1,y), (X-1,y), (x,y+1), or (x,y-1) in the next minute) Tak Es him 1 minute. Some area in the labyrinth contains a bomb-reset-equipment. They could reset the exploding time to 6 minutes.
Given the layout of the Labyrinth and Ignatius ' start position, please tell Ignatius whether he could get out of the The Labyr Inth, if he could, output the minimum time that he had to use to find the exit of the The labyrinth, else output-1.
Here is some rules:
1. We can assume the Labyrinth is a 2 array.
2. Each minute, Ignatius could-only get-one of the nearest area, and he should not walk out of the border, of course he Could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can ' t get out of the The Labyrinth.
4. If Ignatius get to the area which contains bomb-rest-equipment then the exploding time turns to 0, he can ' t use the equ Ipment to reset the bomb.
5. A Bomb-reset-equipment can used as many times as you wish, if it's needed, Ignatius can get to any areas in the lab Yrinth as many times as you wish.
6. The time to reset the exploding time can is ignore, in and words, if Ignatius get to a area which contain bomb-rest- Equipment, and the exploding time is larger than 0, the exploding time would being reset to 6.
Input
The input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case is starts with the integers N and M (1<=n,mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the Labyrinth.
There is five integers which indicate the different type of area in the Labyrinth:
0:the area was a wall, Ignatius should not walk on it.
1:the area contains nothing, Ignatius can walk on it.
2:ignatius ' start position, Ignatius starts his escape from this position.
3:the exit of the Labyrinth, Ignatius ' target position.
4:the area contains a bomb-reset-equipment, Ignatius can delay the exploding time by walking to these areas.
Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should Just output-1.
Sample Input
33 32 1 11 1 01 1 34 82 1 1 0 1 1 1 01 0 4 1 1 0 4 11 0 0 0 0 0 0 11 1 1 4 1 1 1 35 81 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
Sample Output
4-113
Because there is one more time variable, at different times, even in the same position will result in different results. So each state is constructed by a structure of position (x, Y) and time (T) to store each state, traverse all situations, and find the optimal solution.
When a status update occurs, only the longer time remaining can be updated
struct Point { int x, y; int Time ; int dis; Point (intint int int d) { = A; = B; = C; = D; }};
A three-dimensional array was originally used, but timed out ... Can't find the problem, and finally rewrite it.
1 /*2 By:ohyee3 Github:ohyee4 Email:[email protected]5 Blog:http://www.cnblogs.com/ohyee/6 7 かしこいかわいい? 8 エリーチカ! 9 to write out the хорошо code OH ~Ten */ One#include <cstdio> A#include <algorithm> -#include <cstring> -#include <cmath> the#include <string> -#include <iostream> -#include <vector> -#include <list> +#include <queue> -#include <stack> + using namespacestd; A at //DEBUG MODE - #defineDebug 0 - - //Loops - #defineREP (n) for (int o=0;o<n;o++) - in - to Const intMAXN =Ten; + intN, M; - intMAP[MAXN][MAXN]; the * $ Const intdelta[4] = {1,-1,0,0 };Panax Notoginseng - structPoint { the intx, y; + intTime ; A intdis; thePointintAintBintCintd) { +x =A; -y =b; $Time =C; $dis =D; - } - }; the - intBFS (intS1,intS2,intV1,intv2) {WuyiQueue<point>Q; the intMARK[MAXN][MAXN]; -memset (Mark,-1,sizeof(Mark)); Wu intAns =-1; - AboutQ.push (Point (S1, S2,6,0)); $MARK[S1][S2] =6; - while(!Q.empty ()) { -Point temp =Q.front (); - Q.pop (); A + intx =temp.x; the inty =temp.y; - intTime =Temp.time; $ intdis =Temp.dis; the the if(x = = V1 && y = =v2) { theAns = ((ans = =-1) ?(dis): min (ans, dis)); the //printf ("(%d,%d)%d%d\n", X,y,time,dis); - } in the //** theREP (4) { About intxx = x +Delta[o]; the intyy = y + delta[3-O]; the inttt = time-1; the intdd = dis +1; + - if(XX <0|| XX >= N | | yy <0|| YY >=m the|| MAP[XX][YY] = =0|| TT = =0)Bayi Continue; the if(Map[xx][yy] = =4) thett =6; - if(Mark[xx][yy] <TT) { -MARK[XX][YY] =tt; the //printf ("%d%d%d%d\n", XX, yy, TT, DD); the Q.push (Point (xx, yy, TT, DD)); the } the } - } the returnans; the } the 94 voidDo () { the CharS1, v1; the ints2, v2; thescanf"%d%d\n", &n, &m);98 for(inti =0; I < n; i++) About for(intj =0; J < M; J + +) { -scanf"%d", &map[i][j]);101 if(Map[i][j] = =2) {102S1 =i;103S2 =J;104MAP[I][J] =1; the }106 if(Map[i][j] = =3) {107V1 =i;108V2 =J;109MAP[I][J] =1; the }111 } theprintf"%d\n", BFS (S1, S2, V1, v2));113 } the the the intMain () {117 intT;118scanf"%d", &T);119 while(t--) - Do ();121 return 0;122}
HDU 1072.Nightmare