"The Knight walks the board."

Source: Internet
Author: User

/*Knight Walk Board Description: Knight Travel Knight Tour at the beginning of 18th century much more mathematicians and jigsaw puzzle fans attention, when it was proposed has not been tested, the knight's Way for the western chess, the knight can be from any position, it how to walk all the position. Solution: Knight's Walk, basically can be handed back to solve, but the pure recursion in the dimension is quite inefficient, a clever solution by J.cwarnsdorff in 1823, simply said, first will be the most difficult position to finish, the next road is broad, the knight wants the next, for the next no longer The minimum number of steps you can take when you choose. Using this method, in the case of not using recursion, there can be a higher probability of finding the way (there is no chance of finding a walk)*/#include<stdio.h>intpos[8][8] = {0};intTravelint,int);intMain () {intI, J, StartX, Starty;  while(1) {printf ("Enter the starting point:"); scanf ("%d%d", &startx, &starty); if(Travel (StartX, starty)) {printf ("Travel Complete! \ n"); }Else{printf ("Travel Failure! \ n"); }         for(i=0; i<8; i++) {             for(j=0; j<8; J + +) {printf ("%2d", Pos[i][j]); } printf ("\ n"); } printf ("\ n"); }        return 0;}intTravelintXinty) {intI, J, K, L, M; inttmpx, Tmpy; intcount, Min, tmp; //Eight Directions to the knight (clockwise)    intktmovex[8] = {1,2,2,1, -1, -2, -2, -1}; intktmovey[8] = {-2, -1,1,2,2,1, -1, -2}; //Test Next coordinates    intnextx[8] = {0}; intnexty[8] = {0}; //record the number of exits in each direction    intexists[8] = {0}; //starting with 1 mark positioni =x; J=y; POS[I][J]=1; //Traverse the Board     for(m=2; m<= -; m++) {        //Initialize the number of exits in eight directions         for(l=0; l<8; l++) {Exists[l]=0; } l=0;//calculate the direction to go//Test Eight Directions         for(k=0; k<8; k++) {tmpx= i +Ktmovex[k]; Tmpy= j +Ktmovey[k]; //Boundary Skip            if(tmpx<0|| tmpy<0|| Tmpx>7|| Tmpy>7) {                Continue; }            //go to record            if(Pos[tmpx][tmpy] = =0) {Nextx[l]=tmpx; NEXTY[L]=Tmpy; L++;//can go direction plus 1}} Count=l; //no way to go back        if(Count = =0) {            return 0; //One Direction to go Mark}Else if(Count = =1) {min=0; //find out the number of the next position .}Else {             for(l=0; l<count; l++) {                 for(k=0; k<8; k++) {tmpx= Nextx[l] +Ktmovex[k]; Tmpy= Nexty[l] +Ktmovey[k]; if(tmpx<0|| tmpy<0|| Tmpx>7|| Tmpy>7) {                        Continue; }                    if(Pos[tmpx][tmpy] = =0) {Exists[l]++; }                }            }            //find the way to the next location.Min =0; TMP= exists[0];  for(l=0; l<count; l++) {                if(Exists[l] <tmp) {tmp=Exists[l]; Min=l; }            }        }        //use the ordinal mark to pass the positioni =Nextx[min]; J=Nexty[min]; POS[I][J]=m; }    return 1;}

"The Knight walks the board."

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.