Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1254
Push box
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 5181 accepted submission (s): 1439
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 <= 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
Question: If the question is to push the box, you just need to be familiar with the rules for pushing the box. Thought: (1) this question was hard to understand at first, and later I learned a method for status marking, use an array vis [] [] [] [] to mark the current status (the status includes the location of the current person, the location of the current box, and the number of steps of the current push box) (2) because previous statuses may occur during each step, you must mark the nodes in the same status to prevent them from joining the queue. (3) priority queue optimization ~ (4) be careful before ac ~
# Include <iostream> # include <stdio. h> # include <string. h ># include <queue> using namespace STD; int map [10] [10]; int n, m, PX, Py, Sx, Sy, ex, ey; // set global variables, including the initial location of the person, the initial location of the box, and the terminal location bool vis [10] [10] [10] [10] [10]; // flag status int XX [] = {,-}; // set the direction of the array to optimize the wide search int YY [] = }; struct node {int PX, Py; // The Int Sx, Sy; int step; bool operator <(const node & P) const {return p. step <step ;}}; void BFS (int x, int y) {Memset (VIS, false, sizeof (VIS); priority_queue <node> que; node Q, P; q. px = x; q. py = y; q. SX = SX; q. sy = sy; q. step = 0; vis [q. px] [q. PY] [q. SX] [q. sy] = true; que. push (Q); While (! Que. empty () {q = que. top (); que. pop (); If (Q. SX = ex & Q. sy = ey) {printf ("% d \ n", Q. step); Return ;}for (INT I = 0; I <4; I ++) {P. px = Q. PX + XX [I]; p. py = Q. PY + YY [I]; p. SX = Q. SX; p. sy = Q. sy; p. step = Q. step; // determine the feasibility if (P. px <1 | P. px> N | P. PY <1 | P. PY> m) continue; If (Map [p. px] [p. PY] = 1 | Vis [p. px] [p. PY] [p. SX] [p. sy]) continue; If (P. px = P. SX & P. py = P. sy) // find the box, then push it in the forward direction {P. SX = P. SX + XX [I]; p. sy = P. SY + YY [I]; p. step + = 1; // determine the feasibility if (P. SX <1 | P. SX> N | P. sy <1 | P. sy> m) continue; If (Map [p. SX] [p. sy] = 1 | Vis [p. px] [p. PY] [p. SX] [p. sy]) continue;} que. push (p); vis [p. px] [p. PY] [p. SX] [p. sy] = true ;}} printf ("-1 \ n");} int main () {int t; CIN> T; while (t --) {scanf ("% d", & N, & M); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {scanf ("% d", & map [I] [J]); If (Map [I] [J] = 2) {SX = I; Sy = J; // map [I] [J] = 0;} else if (Map [I] [J] = 3) {EX = I; ey = J; // map [I] [J] = 0;} else if (Map [I] [J] = 4) {PX = I; py = J; // map [I] [J] = 0 ;}} BFS (PX, Py) ;}return 0 ;}
HDU 1254 (push box game BFS + status tag)