Problem description is a classic game. today we are going to play a simple version. in a m * n room, there is a box and a porter. the porter's job is to push the box to the specified position. Note that the Porter can only push the box but not pull it, therefore, if the box is pushed to a corner (2), the box cannot be moved. If the box is pushed to a wall, the box can only move along the wall.
Given the structure of the room, the location of the box, the location of the Porter and the location of the box to be pushed, Please calculate the minimum number of bins the porter should push.
The first line of input data is an integer T (1 <= T <= 20), representing the number of test data. then there is the T group of test data. The first row of each group of test data is two positive integers m, n (2 & lt; = m, n <= 7), representing the size of the room, then there is a matrix of m rows and n columns, which represents the layout of the room. 0 represents an empty floor, 1 represents a wall, and 2 represents the start position of the box, 3 indicates the position of the box to be pushed, and 4 indicates the start position of the porter.
Output for each group of test data, the output Porter must push at least the number of cells in the box to help the box push to the specified location. If the box cannot be pushed to the specified location, the output is-1.
Sample Input
15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0
Sample output
4 because only one box is required to reach the destination in the shortest steps, you only need to consider the following points: 1: A box as the subject to find the shortest path for its BFS 2: before moving a box, you must determine whether a person can arrive at the end of the box. 3: A person cannot pass through the box. 4: The box may go through the same code again:# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # define QQ 9 Using namespace STD; int map [QQ] [QQ]; int vis [QQ] [QQ] [4]; // The Box's access array must be three-dimensional, because it may go through the same coordinate int dis [QQ] [QQ]; // human access array int FX [4] [2] = {, 0,-1,-}; struct node {int X, Y, X1, y1, step;}; int M, N; int Sx, Sy, SX1, sy1; int tx1, ty1, TT; bool inmap (int A, int B) {if (a <1 | A> M | B <1 | B> N | map [a] [B] = 1) return false; return true;} void DFS (int xx, in T YY) // can a person arrive at the end of the box {If (xx = tx1 & YY = ty1) {TT = 1; return;} If (TT = 1) return; int Tx, Ty; For (INT I = 0; I <4; I ++) {Tx = xx + FX [I] [0]; ty = YY + FX [I] [1]; if (TX <1 | ty <1 | TX> M | ty> N | map [TX] [ty] = 1 | dis [TX] [ty] = 1) continue; DIS [TX] [ty] = 1; DFS (TX, Ty); DIS [TX] [ty] = 0 ;}} void BFS () {queue <node> q; node now, next; memset (VIS, 0, sizeof (VIS); memset (DIS, 0, sizeof (DIS); now. X = Sx, now. y = sy; now. x1 = SX1, now. y1 = sy1; now. s TEP = 0; q. Push (now); While (! Q. empty () {now = Q. front (); q. pop (); If (Map [now. x] [now. y] = 3) {cout <now. step <Endl; return ;}for (INT I = 0; I <4; I ++) {next. X = now. X + FX [I] [0]; next. y = now. Y + FX [I] [1]; tx1 = now. x-Fx [I] [0]; ty1 = now. y-Fx [I] [1]; next. step = now. step + 1; if (inmap (next. x, next. y) & inmap (tx1, ty1) & map [next. x] [next. y]! = 1 & Vis [next. x] [next. y] [I] = 0) {memset (DIS, 0, sizeof (DIS); TT = 0; DIS [now. x1] [now. y1] = 1; DIS [now. x] [now. y] = 1; DFS (now. x1, now. y1); // important people cannot pass through the box dis [now. x1] [now. y1] = 0; DIS [now. x] [now. y] = 0; If (TT = 1) {vis [next. x] [next. y] [I] = 1; next. x1 = tx1; next. y1 = ty1; q. push (next) ;}}}cout <-1 <Endl; return ;}int main () {int T, I, j; scanf ("% d ", & T); While (t --) {scanf ("% d", & M, & N); for (I = 1; I <= m; I ++) for (j = 1; j <= N; j ++) {scanf ("% d", & map [I] [J]); if (Map [I] [J] = 2) SX = I, Sy = J; If (Map [I] [J] = 4) SX1 = I, sy1 = J;} BFS ();} return 0 ;}