Horse Jumping in Chinese chess
Description
The current Board size is not necessarily given by p and q, and obstacles will appear in the Board (restrict the horse's action, the same as playing chess)
Input
Input N in the first line indicates there are N groups of test data.
Enter two integers p and q In the first line of each group of test data, indicating the size of the Board (1 <= p, q <= 100 ).
Enter four integers in the second row of each group of test data, indicating the start and end positions of the horse. (The value range of the position is the same as that of p and q)
Input m in the third row to indicate the number of obstacles in the figure.
Then follow the M line to indicate the coordinates of the obstacle.
Output
The minimum number of steps required for the horse to move from the start point to the end point.
If the horse cannot reach the end point, enter "can not reach !"
Sample input 2 9 10 1 1 2 3 0 9 10 1 1 2 3 8 1 2 2 2 2 3 3 3 4 4 3 2 2 4 13 sample output 1 can not reach! Prompt
This is a search question. You can use DFS or BFs. We recommend that you select BFs ). Add the starting point of the horse to the queue at the beginning, and then use the broad search idea to add other points that can be reached to the queue. Here an array is required to record whether the point has been added to the queue before, if you have joined the queue, you do not need to add it again until the elements in the queue are empty or the end point is searched. Then, the search stops and the corresponding answer is output.
The reference code is as follows:
# Include <stdio. h> # define Max 150 # define size 10201 # define OK 1 // number of places where the horse can jump in the Next Step # define M 8 // relative horizontal and vertical coordinates of the location where the horse can jump, upper, right, bottom, left int movex [m] = {-1, 1, 2, 1,-1,-2,-2 }; int Movey [m] = {2, 2, 1,-1,-2,-2,-1 }; // The maximum number of obstacles encountered during horse jumping # define B 4 // The relative total abscissa of the horse's obstacle position, upper, right, bottom, left int barx [B] = {, 0,-1}; int Bary [B] = }; typedef struct {int step; // number of record steps int flag; // tag} chessboard; // board type typedef struct {int lnum; int rnum; // record element subscript} Queue; // queue type: queue [size]; // queue int rear, front; // queue pointer chessboard [Max] [Max]; int A, B, C, d, row, line; // start point, end coordinate, and column value int BFS () {int x0, y0; int MX, my, BX, by; int I; rear =-1; front =-1; // queue pointer initialization Board [a] [B]. step = 0; // The number of steps from the start point is counted as 0 Board [a] [B]. flag = 1; // start point mark already entered the queue rear ++; queue [rear]. lnum = A; queue [rear]. rnum = B; // the starting point of the advanced queue while (front! = Rear) // The queue is not empty {front ++; X0 = queue [Front]. lnum; Y0 = queue [Front]. rnum; // output queue if (X0 = C & Y0 = d) Return OK; // if the end is found, stop searching for (I = 0; I <m; I ++) // M = 8, there are eight places to jump {// calculate the value, use this value to determine whether there are obstacles to jump in this direction. bx = x0 + barx [I/2]; by = y0 + Bary [I/2]; // used to determine whether there are obstacles // calculate the value, this value is used to determine whether there is an obstacle at the endpoint of the word in this direction or whether it has passed through // The coordinate MX = x0 + movex [I] at the next position; my = y0 + Movey [I]; If (Board [BX] [by]. flag! =-1) {If (MX> 0 & MX <= row & my> 0 & my <= line &&! Board [MX] [my]. flag) // the error message returned when the error message returned. flag must be 0 {rear ++; queue [rear]. lnum = Mx; queue [rear]. rnum = My; // enter the queue Board [MX] [my]. flag = 1; // mark as having passed through the Board [MX] [my]. step = Board [x0] [y0]. step + 1; // The path value is the root path value plus 1} // If} // For} // while return 0;} int main () {int n, m, E, F; int K, I, j; scanf ("% d", & N ); // n groups of test data in total for (k = 0; k <n; k ++) {scanf ("% d", & Row, & line ); // enter the size of the row and column for (I = 1; I <= row; I ++) for (j = 1; j <= line; j ++) board [I] [J]. flag = 0; // initialization, full Mark as 0 scanf ("% d", & A, & B, & C, & D ); // enter the start point and end point coordinate value scanf ("% d", & M); // m Obstacle for (I = 0; I <m; I ++) {scanf ("% d", & E, & F); Board [E] [f]. flag =-1; // obstacle points are marked as-1} If (BFS () printf ("% d \ n", Board [C] [D]. step); else printf ("can not reach! \ N ") ;}return 0 ;}