Push Box

**Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)**

Total submission (s): 6416 Accepted Submission (s): 1834

Problem description Push box is a very classic game. Let's play a simple version today. In a m*n room there is a box and a porter, the Porter's job is to push the box to the designated position, note that the porter can only push the box and not pull the box, So if the box is pushed to a corner (2) then the box can no longer be moved, and if the box is pushed onto a wall, the box can only move along the wall.

Now given the structure of the room, the position of the box, the position of the porter and the position to be pushed away, please calculate how many boxes the porter should push the box at least.

The first line of input data is an integer T (1<=t<=20), which represents the number of test data. Then the T group test data, the first line of each set of test data is two positive integer m,n (2<=m,n<=7), representing the size of the room, Then there is a M-row N-column matrix representing the layout of the room, where 0 represents an empty floor, 1 represents a wall, 2 represents the starting position of the box, 3 represents the position to be pushed, and 4 represents the starting position of the porter.

Outputs for each set of test data, the output porter needs to push the box at least to get the box to the specified position, and output-1 if it cannot be pushed to the specified position.

Sample Input15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0

Sample Output4

AUTHORIGNATIUS.L & Weigang Lee

RECOMMENDIGNATIUS.L | We have carefully selected several similar problems for you:1180 1072 1372 1429 1728 when I first wrote this question, I thought it was simple, if the box could move, then move in the direction The front of the box must be a vacant space, and then I think of it as long as the box moves in front of a point where the empty box can move in this direction ... So I finished writing the code, over the test data of the super-water, and then gorgeous wa ... Thought carefully, originally I did not consider people can walk over .... Cough, change the thought, start again, completely understand test instructions, feel a bit complicated, can't help a big head, ready to search a code on the Internet, understand the method with four-dimensional array marker, suddenly have an idea, completed their own code. The code is a lot of quantitative, must be careful. The first wide search box can go to the location, and then deep search whether people can reach the point of pushing the box, remember to open the box into a wall. Test instructions: Chinese question, very good understanding. Attached code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 using namespacestd;6 structnode7 {8 intx, y;//the location of the box9 intXx,xy;//person's positionTen intT//the number of boxes moving One } s1,s2; A intf[4][2]= {0,1,0,-1,1,0,-1,0};//direction Variable - intmap[Ten][Ten];//Record Map - intvis[Ten][Ten][Ten][Ten];//four-dimensional array markers, while recording the position of the box and the position of the person, remove duplicate the intN,m;//Border - intA1,A2,B1,B2;//Initial value - intflag[Ten][Ten],kk; - + BOOLxxintAintb) - { + if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]!=1)return true; A return false; at } - - voidDFS (intNxintNyintMxintmy) - { - if(nx==mx&&ny==my)//If you can find the location of this person from the point you want, you can come - { inkk=1;//If you can walk, mark it as 1 . - return; to } + for(intI=0; i<4&&!kk; i++) - { the intx=nx+f[i][0]; * inty=ny+f[i][1]; $ if(XX (x, y) &&!Flag[x][y])Panax Notoginseng { -flag[x][y]=1;//the points passed are marked 1 theDFS (x,y,mx,my);//Continue search + } A } the } + - voidBFS () $ { $Queue<node>Q; - while(!q.empty ()) - Q.pop (); the intNx,ny; -S1.X=A1;//InitializeWuyis1.y=B1; thes1.xx=A2; -s1.xy=B2; Wus1.t=0; -vis[a1][b1][a2][b2]=1;//start status marked as already present About Q.push (S1); $ while(!q.empty ()) - { -s1=Q.front (); - Q.pop (); A if(map[s1.x][s1.y]==3)//the queue loop ends when the box arrives at the specified position + { theprintf"%d\n", s1.t); - return; $ } the for(intI=0; i<4; i++) the { thes2.x=s1.x+f[i][0];//position after the box is moved thes2.y=s1.y+f[i][1]; -nx=s1.x-f[i][0];//If the box can be moved to that position, one must be able to reach this point . inny=s1.y-f[i][1]; the if(XX (S2.X,S2.Y) &&xx (nx,ny) &&!Vis[s2.x][s2.y][nx][ny]) the //The xx () function determines whether the hyper-boundary and whether it is a wall, the position after the box is moved and the position to which the person is to be satisfied, and the state is not present at this time About { thememset (Flag,0,sizeof(flag));//all the points in the map that are not walls can go . theflag[nx][ny]=flag[s1.x][s1.y]=1;//The position before the box is moved and the place where the person is going is considered a wall . thekk=0;//whether the marker can come along +DFS (NX,NY,S1.XX,S1.XY);//Deep Search Query - if(KK)//if people can walk to the {Bayivis[s2.x][s2.y][nx][ny]=1;//This status token is already present theS2.xx=nx;//record where the person is at this time thes2.xy=NY; -s2.t=s1.t+1;//number of steps plus one -Q.push (S2);//into the queue the } the } the } the } -printf"-1\n");//if not, then output-1 the return; the } the intMain ()94 { the intt,i,j; thescanf"%d",&T); the while(t--)98 { Aboutscanf"%d%d",&n,&m); -memset (Vis,0,sizeof(VIS));//Mark all initialized to 0101 for(i=0; i<n; i++)102 for(j=0; j<m; J + +)103 {104scanf"%d",&map[i][j]); the if(map[i][j]==2)//record the starting position of the box106 {107a1=i;108b1=J;109 } the if(map[i][j]==4)//record the starting position of the person111 { theA2=i;113B2=J; the } the } the BFS ();117 }118 return 0;119}

Push box (nested search, DFS in BFS)