ACM-backtracking algorithm

Source: Internet
Author: User

Example: Question about the total number of Different horse steps: On a 4*5 board, the start coordinate (vertical, horizontal) of the horse is input by the keyboard, seek the horse to return the total number of different steps in the initial position ).

Algorithm Analysis: because the size of the Board is only 4*5, you only need to use the backtracking algorithm to search for all the different paths in the initial position. The efficiency basically meets the requirements. The Recursive Backtracking algorithm can be described as procedure search (now: position ); {now is the current position} The begin for horse starts from the current position now and goes one step in place. Set each of the following steps: Do begin if next in the checker and next without passing then if next = Start Point the total number of then different steps plus 1 else begin mark next has passed; search (next); unmark the position next; end; the specific implementation of the algorithm is discussed below. The checkerboard is represented by coordinates. p (x, y) indicates any point on the checkerboard. The range of X and Y is: 1 <= x <= Y <= 5.
Starting from p (x, y), the next step has a maximum of eight locations, which are marked as P1, P2, „, p8. If K represents these eight directions, K =, ..., 8. That is, the horse starts from point P and first travels in the direction of k = 1. When all the different steps are completed in this direction, the horse goes back and changes K = 2 to continue „„
Coordinate Calculation of each point. If the coordinates of point P are (x, y), the coordinates of point P are p1 (x + 1, y-2), P2 (x + 2, Y-1 ),„, p7 (X-2, Y-1), p8 (x-1, y-2 ). To simplify coordinate calculation, an incremental array is introduced:
Direction: array [1 .. 8] of position =
(X: 1; Y:-2), (X: 2; Y:-1), (X: 2; Y: 1), (X: 1; Y: 2 ),
(X:-1; Y: 2), (X:-2; Y: 1), (X:-2; Y:-1), (X:-1; y:-2 ));
Then, the coordinates for K to reach the point are: PK (x + Direction [K]. X, Y + Direction [K]. Y ). The procedure is as follows:

# Include <cstdlib>
# Include <iostream>
Using namespace STD;
# Define row 4
# Define line 5
Struct position
{
Int X, Y;
};
Const position Direction [8] =
{(1,-2), (2,-1), (-2, -1), (-1,-2 )};
Int pass [4] [5];
Position start;
Int total;
Void search (position now)
{
Int I;
Position next;
For (I = 0; I <8; I ++)
{
Next. x = now. x + Direction [I]. X;
Next. Y = now. Y + Direction [I]. Y;
If (next. x> = 0) & (next. x <= row-1) & (next. y> = 0) & (next. Y <= line-1) & (pass [next. x] [next. y]) = 0)
If (next. x = start. X) & (next. Y = start. y ))
Total ++;
Else
{
Pass [next. x] [next. Y] = 1;
Search (next );
Pass [next. x] [next. Y] = 0;
}
}
}

Int main (INT argc, char * argv [])
{
Total = 0;
For (INT I = 0; I <row; I ++)
For (Int J = 0; j <line; j ++)
{
Pass [I] [J] = 0;
}
Cout <"input start position" <Endl;
Cin> Start. x> Start. Y;
Search (start );
Cout <"Total =" <total <Endl;
System ("pause ");
Return exit_success;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.