It's still AC at last, but Wa has done it several times. Alas, I ignored a key point, that is, when I searched for it extensively, one point can be passed more than once. It's so depressing, indeed, I read the test example provided by Daniel, passed this example, and then AC
You need to open another array record to reach this point.
The subject uses BFs to enumerate the statuses of all push points. DFS is used to determine whether the push box worker can reach the back of the box.
Let's take a look at this test example.
5 3
0 0 0
0 0 0
0 2 0
1 4 1
1 3 1
The answer is 4.
# Include <iostream>
# Include <queue>
Using namespace STD;
Int map [8] [8], n, m, EI, EJ, A, B, dir [4] [2] = }, {0, 1}, {0,-1 }};
// (EI, EJ) indicates the end point of each DFS, and (a, B) indicates the start position of the push box worker.
Bool vis [8] [8] [4];
Bool vis1 [8] [8];
Struct Node
{
Int X, Y, CNT, di;
Friend bool operator <(const node & C, const node & D)
{
Return C. CNT> D. CNT;
}
};
Node F;
Int DFS (int I, Int J)
{
If (I = EI & J = EJ)
Return 1;
For (int K = 0; k <4; k ++)
{
Int c = I + dir [k] [0];
Int d = J + dir [k] [1];
If (C> N | C <1 | D> M | D <1 | map [C] [d] = 1 | vis1 [C] [d ])
Continue;
Vis1 [C] [d] = 1;
If (DFS (c, d) return 1;
}
Return 0;
}
Void BFS ()
{
Priority_queue <node> q;
Q. Push (f );
While (! Q. Empty ())
{
Node T = Q. Top (), temp;
Q. Pop ();
For (int K = 0; k <4; k ++)
{
Int I = T. X + dir [k] [0];
Int J = T. Y + dir [k] [1];
Ei = T. X-dir [k] [0]; // indicates the rear of the box to be pushed in this direction
EJ = T. Y-dir [k] [1];
If (I> N | I <1 | j> M | j <1 | Vis [I] [J] [k] | map [I] [J] = 1) continue;
If (EI> N | EI <1 | EEJ> M | EEJ <1 | map [ei] [EEJ] = 1) continue;
Memset (vis1, 0, sizeof (vis1 ));
If (T. Di! =-1)
{
// (T. x, t. y) represents the position of the box at the moment, (T. x-dir [T. di] [0], T. x-dir [T. di] [1]) indicates the location of a person
Vis1 [T. x] [T. y] = 1; vis1 [T. x-dir [T. di] [0] [T. y-dir [T. di] [1] = 1;
If (! DFS (T. X-dir [T. Di] [0], T. Y-dir [T. Di] [1]) continue;
}
Else {
Vis1 [a] [B] = 1; vis1 [T. X] [T. Y] = 1;
If (! DFS (a, B) continue;
}
If (Map [I] [J] = 3)
{
Cout <t. CNT + 1 <Endl;
Return;
}
If (Map [I] [J] = 0 | map [I] [J] = 4 | map [I] [J] = 2)
{
Temp. CNT = T. CNT + 1; temp. DI = K;
Temp. x = I; temp. Y = J;
Q. Push (temp );
Vis [I] [J] [k] = 1;
}
}
}
Cout <-1 <Endl;
}
Int main ()
{
Int CAS;
Cin> CAS;
While (CAS --)
{
Cin> N> m;
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= m; j ++)
{
Cin> map [I] [J];
If (Map [I] [J] = 2)
F. X = I, F. Y = J;
If (Map [I] [J] = 4)
A = I, B = J;
}
F. CNT = 0; f. DI =-1;
Memset (VIS, 0, sizeof (VIS ));
BFS ();
}
Return 0;
}