Nightmare time limit: +Ms | Memory Limit:65535KB Difficulty:4
-
-
Describe
-
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.
-
-
-
-
Each test case starts with 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 be five integers which indicate the different type of area in the Labyrinth:
0:the area is a wall, Ignati US should not walk on it.
1:the area contains nothing, Ignatius can walk on it.
2:ignatius ' start position, Ignatius starts his escape from the 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
-
-
2
-
-
3 3
-
-
2 1 1
-
-
1 1 0
-
-
1 1 3
-
-
4 8
-
-
2 1 1 0 1 1 1 0
-
-
1 0 4 1 1 0 4 1
-
-
1 0 0 0 0 0 0 1
-
-
-
-
Sample output
-
-
4
-
-
-1
-
-
Uploaded by
Acm_ Zhiqianglin
The title of the topic is not marked, but note that 4 of the case should be marked as 1;
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespaceStd
intmap[ Ten][ Ten], ac[ 4][ 2] = { 0, 1, 0, - 1, - 1, 0, 1, 0};
structMaze
{
intx, y, step, Eng;
} R, S, T;
intN, M;
intBfs ( intA intb
{
R.x = A; R.y = b; R.step = 0; R.eng = 6;
Queue<maze> Q;
Q.push (R);
while(! Q.empty ())
{
s = Q.front (); Q.pop ();
for( inti = 0; I < 4; i++)
{
T.x = s.x + ac[i][ 0];
T.y = S.y + ac[i][ 1];
T.step = S.step + 1;
T.eng = S.eng- 1;
if(T.x >= 0&& t.x < n && t.y >= 0&& T.y < M && Map[t.x][t.y]! = 0&& T.eng > 0)
{
if(Map[t.x][t.y] = = 3)
returnT.step;
if(Map[t.x][t.y] = = 4)
{
MAP[T.X][T.Y] = 1; /***********************/
T.eng = 6;
}
Q.push (t);
}
}
}
return- 1;
}
intMain ()
{
intT
scanf " %d ", &t);
while(t--)
{
intX, Y, I, J;
scanf " %d%d ", &n, &m);
for(i = 0; I < n; i++)
for(j = 0; J < M; J + +)
{
scanf " %d ", &map[i][j]);
if(Map[i][j] = = 2)
{
x = i;
y = j;
}
}
printf " %d\n ", Bfs (x, y));
}
return 0;
}
Hangzhou Electric 483--nightmare (BFS)