Knight travel-(0)

Source: Internet
Author: User

/* Aegis travel */
Int qp [9] [9] = {0};/* Board */
Int bz [65] [2] = {0};/* save step */
Int fx [9] = {0,-2,-1, 1, 2, 2, 1,-1,-2};/* Store X coordinates in 8 directions */
Int fy [9] = {0, 1, 2, 2, 1,-1,-2,-2,-1};/* store Y coordinates in 8 directions */
/* The priority of each position on the xh memory board. 9 indicates the highest level, and 0 indicates the lowest level */
Int xh [9] [9] = {0, 0}, {, 9, 3, 3, 4, 5, 8 },
{, 7}, {, 6}, {, 6 },
{, 7}, {, 8}, {, 6, 9 }};
Int round (int x, int y)/* when the first step is reached, check whether the first step is returned. If yes, 1 */is returned */
{Int a, B, I;
For (I = 1; I <9; I ++)
{A = x + fx [I]; B = y + fy [I];
If (qp [a] [B] = 1 & a> 0 & a <9 & B> 0 & B <9) return (1 );
}
Return (0 );
}
Void check (int x, int y, int * t)/* check the feasibility and priority of the position (X, Y) in 8 directions, and sort it to t */
{Int a, B, I, j, k [9] = {0 };
For (I = 1; I <9; I ++)
{A = x + fx [I]; B = y + fy [I];
If (a> 0 & a <9 & B> 0 & B <9 & qp [a] [B] = 0) k [I] = xh [a] [B] + 1;/* If feasible, the priority is determined */
}
For (I = 0; I <8; I ++)/* sort by priority and Feasible Direction */
{For (B = 0, j = 0, a = 1; a <9; a ++)
If (k [a]! = 0 & k [a]> j) {B = a; j = k [a];}
If (j! = 0) {t [I] = B; k [B] = 0 ;}
}}
Int bl (int x, int y, int n)/* "traveling" function */
{Int I, j, t [8] = {0 };
Bz [n] [0] = x; bz [n] [1] = y; qp [x] [y] = n ++;/* record current position */
If (n = 65)/* if it is already 64 steps, check whether it is traveling */
If (round (x, y) return (1);/* Indicates traveling, return 1; otherwise return 0 */
Else return (0 );
Check (x, y, t);/* determine the feasibility and priority of the next eight directions. The feasible direction after sorting by priority size exists in array t */
For (I = 0; t [I]! = 0; I ++)
{J = t [I];
If (bl (x + fx [j], y + fy [j], n) return (1);/* recursively check whether the next step can be performed */
}
For (n --, I = n; bz [I] [0]! = 0; bz [I] [0] = bz [I] [1] = 0, I ++)/* backtracing: erase the path that has been taken before */
Qp [bz [I] [0> [bz [I] [1> = 0;
Return (0 );
}
Main ()
{Int x, y;
X = bl (, 1);/* travel from */
For (printf ("n"), x = 1; x <9; x ++, printf ("n")/* Print Output */
For (y = 1; y <9; printf ("% 4d", qp [x] [y ++]);
}

Related Article

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.