Backtracking Method-horse barrier

Source: Internet
Author: User

Question:

On the board, point A has a stroke and needs to go to point B. Walking rules:
It can be down or right. At the same time, there is a horse from the other side on the keyboard,
The point where the horse is located and all the points that can be jumped in one step are called the control points of the other horse.
Therefore, it is called "The horse stops the river ".
The checkerboard is represented by coordinates. Point A (0, 0), point B (n, m) (n, m is an integer not greater than 16 ),
The coordinates of the horse locations must be given. Now you need to calculate the number of paths from point A to point B,
Assuming that the position of the horse is fixed, it is not a step by step.

Ideas:

This question can naturally come up with solutions using the Backtracking Method. The backtracking method is effective in solving grid-like algorithm problems. When solving a specific problem, first mark the location (the day of the horse) that the cursor cannot reach based on the position of the horse. In the current position, if the cursor can be moved to the right, it will be recursively shifted to the right. If it can be moved down, recursively moves down. If the target is reached, the recursion is terminated. It should be noted that it is natural to use recursion to express the Backtracking Method. Because the Backtracking Method takes precedence over depth, so does recursion.

The program source code is as follows:

# Include "stdlib. H"
# Include "stdio. H"
 
Static int path [30] = {0};/* indicates the record path. If the value is 1, it indicates the right. If the value is 2, it indicates 16*2-2 = 30 */
Int pathnum = 0;/* record path length */
Static int mesh [16] [16] = {0};/* 16*16 chessboard */
Int BX, by, CX, Cy;/* records the position of B and C. The position of A is (0, 0 )*/

/* Set the control position of the horse */
Void setconfig ()
{
/* Determine whether the C position is valid */
If (CX <0 & CX> 15 & Cy <0 & Cy> 15)
Printf ("the location of C is wrong! /N ");

/* Analyze the control position of the horse and mark it on the Board */
Mesh [CX] [Cy] = 1;
If (CY> = 2)
{
If (CX> = 1)
Mesh [Cx-1] [Cy-2] = 1;
If (CX <= 14)
Mesh [cx + 1] [Cy-2] = 1;
}
If (CY <= 13)
{
If (CX> = 1)
Mesh [Cx-1] [Cy + 2] = 1;
If (CX <= 14)
Mesh [cx + 1] [Cy + 2] = 1;
}
If (CX> = 2)
{
If (CY> = 1)
Mesh [Cx-2] [Cy-1] = 1;
If (CY <= 14)
Mesh [Cx-2] [Cy + 1] = 1;
}
If (CX <= 13)
{
If (CY> = 2)
Mesh [cx + 2] [Cy-1] = 1;
If (CY <= 13)
Mesh [cx + 2] [Cy + 1] = 1;
}
}

/* Print path */
Void printpath (int p)
{
Printf ("PATH % d is as followed:/N", pathnum );
Int K = 0;
For (k = 0; k <p; k ++)
{
If (path [k] = 1)
Printf ("right ");
If (path [k] = 2)
Printf ("down ");
}
Printf ("/n ");
}

/* Recursive search */
Void dorecuresearch (int I, Int J)
{
If (mesh [I] [J] = 2)/* reach point B */
{
Pathnum ++;
Printpath (I + J );
Return;
}

If (I <BX & mesh [I + 1] [J]! = 1)/* extend to the right */
{
Path [I + J] = 1;
Dorecuresearch (I + 1, J );
}

If (j <by & mesh [I] [J + 1]! = 1)/* extend down */
{
Path [I + J] = 2;
Dorecuresearch (I, j + 1 );
}
}

/* Search */
Void search ()
{
/* Determine whether location B is valid */
If (BX <0 & BX> 15 & by <0 & by> 15)
{
Printf ("the location of C is wrong! /N ");
Return;
}

/* Determine whether the position of B is in the control position of the horse */
If (mesh [BX] [by] = 1)
{
Printf ("the B is never reached! /N ");
Return;
}

If (mesh [0] [0] = 1)
{
Printf ("the is never begin! /N ");
Return;
}

Mesh [BX] [by] = 2;/* mark the end position */
/* Print the position of the board and A, B, and C */
Int I, J;
For (I = 0; I <16; I ++)
{
For (j = 0; j <16; j ++)
{
If (I = Bx & J =)
Printf ("B ");
Else if (I = 0 & J = 0)
Printf ("");
Else if (mesh [I] [J] = 1)
Printf ("C ");
Else
Printf ("0 ");
}
Printf ("/N ");
}

/* Recursive solution */
Dorecuresearch (0, 0 );
Printf ("there is % d paths/N", pathnum );
}

Int main ()
{
/* Get input */
Printf ("Please input the location of B/N ");
Scanf ("% d", & BX );
Scanf ("% d", & );
Printf ("Please input the location of C/N ");
Scanf ("% d", & CX );
Scanf ("% d", & CY );

/* Solve and print */
Setconfig ();
Search ();
System ("pause ");
Return 0;
}
 

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.