On a map of n × m, 0 indicates a wall, 1 indicates a blank space, 2 indicates a person 3 indicates a destination, and 4 indicates a bomb reboot. The time required for a bomb is 6. The time required for a person to take a step is the shortest time required for a person to step from 2 to 3.
[Csharp]
# Include "stdio. h"
# Include "string. h"
# Include "queue"
Using namespace std;
Struct node
{
Int x, y, step, count;
} P, q;
Int d [4] [2] = {,-, 0,-1 };
Int map [10] [10], sx, sy, n, m;
Int judge (int x, int y)
{
If (x> = 1 & x <= n & y> = 1 & y <= m & map [x] [y]! = 0)
Return 1;
Return 0;
}
Void bfs ()
{
Queue <node> Q;
Int I;
P. x = sx;
P. y = sy;
P. step = 0;
P. count = 6;
Q. push (p );
Map [sx] [sy] = 0;
While (! Q. empty ())
{
P = Q. front ();
Q. pop ();
For (I = 0; I <4; I ++)
{
Q = p;
Q. x + = d [I] [0];
Q. y + = d [I] [1];
If (judge (q. x, q. y ))
{
If (map [q. x] [q. y] = 1)
{
Q. count --;
If (q. count> = 1)
{
Q. step ++;
Q. push (q );
}
}
If (map [q. x] [q. y] = 4)
{
Q. count --;
If (q. count> = 1)
{
Q. step ++;
Q. count = 6;
Q. push (q );
}
Map [q. x] [q. y] = 0;
}
If (map [q. x] [q. y] = 3 & q. count> 1)
{
Q. step ++;
Printf ("% d \ n", q. step );
Return;
}
}
}
}
Printf ("-1 \ n ");
}
Int main ()
{
Int T, I, j;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% d", & n, & m );
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= m; j ++)
{
Scanf ("% d", & map [I] [j]);
If (map [I] [j] = 2)
{
Sx = I; sy = j;
}
}
}
Bfs ();
}
Return 0;
}
Author: yyf572132811