Knight parade problem source code C language description

Source: Internet
Author: User
//////////////////////////////////////// //////////////////////////////////
/* About server guard parade */
//////////////////////////////////////// /////////////////////////////////
 
# Include <stdio. h>
Int f [11] [11];/* define a matrix to simulate the chessboard */
Int adjm [121] [121];/* indicates the flag matrix.
1-1-21 (Row-first) adjm [I] [J] = 1 */
 
Void creatadjm (void);/* Create a flag Matrix Function Declaration */
Void mark (INT, Int, Int, INT);/* set the position of the Flag matrix to 1 */
Void travel (INT, INT);/* cruise function declaration */
Int n, m;/* defines the size of the matrix and the size of the matrix */
 
 
/****************************** Main function ******* ****************************/
Int main ()
{
Int I, J, K, L;
Printf ("Please input size of the chessboard:");/* size of the input matrix */
Scanf ("% d", & N );
M = N * N;
Creatadjm ();/* Create a matrix */
Puts ("the Sign Matrix is :");
For (I = 1; I <= m; I ++)/* print the output flag matrix */
{
For (j = 1; j <= m; j ++)
Printf ("% 2D", adjm [I] [J]);
Printf ("/N ");
}

Printf ("Please input the knight's position (I, j):");/* enter the initial position of the server guard */
Scanf ("% d", & I, & J );
L = (I-1) * n + J;/* the abscissa of the logo matrix corresponding to the current position of the server guard */
While (I> 0) | (j> 0)/* server guard position judgment */
{
For (I = 1; I <= N; I ++)/* board matrix initialization */
For (j = 1; j <= N; j ++)
F [I] [J] = 0;
K = 0;/* Number of hops */
Travel (L, k);/* starting from I, j */
Puts ("the travel steps are :");
For (I = 1; I <= N; I ++)/* output the parade process after the parade is completed */
{
For (j = 1; j <= N; j ++)
Printf ("% 4D", F [I] [J]);
Printf ("/N ");
}

Printf ("Please input the knight's position (I, j):");/* enter the start position for the next parade */
Scanf ("% d", & I, & J );
L = (I-1) * n + J;
}
Puts ("/n press any key to quit ...");
// Getch ();
Return 0;
}
 
 
 
/****************************** Create the symbol matrix subfunction ***** ********************/
Void creatadjm ()
{
Int I, J;
For (I = 1; I <= N; I ++)/* initialize the parade matrix */
For (j = 1; j <= N; j ++)
F [I] [J] = 0;
For (I = 1; I <= m; I ++)/* Sign Matrix initialization */
For (j = 1; j <= m; j ++)
Adjm [I] [J] = 0;
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
If (F [I] [J] = 0)/* Set 1 for all elements of the qualified flag matrix */
{
F [I] [J] = 1;
If (I + 2 <= N) & (J + 1 <= n) mark (I, j, I + 2, J + 1 );
If (I + 2 <= N) & (J-1> = 1) mark (I, j, I + 2, J-1 );
If (I-2> = 1) & (J + 1 <= n) mark (I, j, I-2, J + 1 );
If (I-2> = 1) & (J-1> = 1) mark (I, j, I-2, J-1 );
If (J + 2 <= N) & (I + 1 <= n) mark (I, j, I + 1, J + 2 );
If (J + 2 <= N) & (I-1> = 1) mark (I, j, I-1, J + 2 );
If (J-2> = 1) & (I + 1 <= n) mark (I, j, I + 1, J-2 );
If (J-2> = 1) & (I-1> = 1) mark (I, j, I-1, J-2 );
}
Return;
}
 
 
/************************************ Parade subfunctions *** ****************************/
Void travel (int p, int R)
{
Int I, j, Q;
For (I = 1; I <= N; I ++)
For (j = 1; j <= N; j ++)
If (F [I] [J]> r) f [I] [J] = 0;/* When the Board matrix is set to R, set it to 0 */
R = R + 1;/* Skip count plus 1 */
I = (p-1)/n) + 1;/* restore the abscissa of the chessboard matrix */
J = (p-1) % n) + 1;/* restore the ordinate coordinate of the chessboard matrix */
F [I] [J] = r;/* use F [I] [J] as the destination of Step r */

 
 
For (q = 1; q <= m; q ++)/* start the test parade from all possible conditions */
{
I = (q-1)/n) + 1;
J = (q-1) % n) + 1;
If (adjm [p] [Q] = 1) & (f [I] [J] = 0 ))
Travel (Q, R);/* recursively call itself */
}
Return;
}
 
*********** ****************************/
Void mark (INT I1, int J1, int I2, int J2)
{
Adjm [(i1-1) * n + J1] [(i2-1) * n + J2] = 1;
Adjm [(i2-1) * n + J2] [(i1-1) * n + J1] = 1;
Return;
}
 

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.