"Data structure and algorithm analysis-C language Realization" horseshoe board

Source: Internet
Author: User

Problem description

The chess chessboard is an 8x8 checkered checkerboard. Now the "horse" is placed in any designated box, according to the "horse" moves rules to move the "horse". Each square is required to enter only once, eventually allowing the "horse" to travel across the 64 squares of the chessboard.

Write a C program, to achieve the horse board operation, the need to use the 1?64 64 numbers to mark the path of the horse, that is, according to the route to be found, the number 1, 2, ... 64 fill in the grid of the chessboard and output.

Problem analysis

In chess, the "horse" movement rule 1 is shown.

1, the solid circle in the figure represents the position of the "horse", and the next step is to move to the 8 positions indicated by the hollow circle in the figure, which is called "Horse Walking Day". However, if the "horse" is located near the boundaries of the chessboard, then it is not necessarily 8 where the next step can be moved, because the "horse" is guaranteed to walk in the chessboard every step of the way.

The problem with the horse board is actually to fill the 1,2,......,64 into an 8x8 matrix, requiring the adjacent two numbers to be placed in the matrix according to the "horse" move rule.

For example, the number A is placed in the matrix (i,j) position, the number a+1 can only be placed in the matrix (i-2,j+1), (i-1,j+2), (i+1,j+2), (i+2,j+1), (i+2,j-1), (i+1,j-2), (i-1,j-2), (I-2, J-1) in one place. Fills and outputs the matrix.

So in the matrix from the ... Traversing to 64, we get the walking route of the horse tread board. So the ultimate goal of the subject is to output a 8*8 matrix, which is filled with 1, 2 ... 64 of these 64 numbers, the adjacent numbers follow the rules of "horse walking Day".

Algorithm design

A relatively easy way to understand the problem of horse stepping board is to apply recursive depth-first search idea. Because "horse" every step is blind, it can not judge the current steps must be correct, but only to ensure that the current step is to go.

Every move of the horse moves from its current position to the 1 in the next 8 positions (in the case where it is next to 8 positions). So the path currently taken by the horse is not necessarily correct, as it may have the remaining optional path without trying the "walking process is actually a process of deep exploration." The root node of the "Explore tree" is the initial position of "horse" in the chessboard.

Then the horse has two ways of walking, and the root node derives two branches. And then walk, the root node of the two children can be derived from the other different "walking route" branch, so derived from the "horse" all the possible steps of the state.

As you can see, the leaf node of the exploration tree may only have two states: one is that the node can no longer derive other "walking" branches, that is, the "horse" is not able to go through, and the other is that every square in the chessboard has been walked, that is, "horse" tread the chessboard. So the path from the root node of the exploration tree to the leaf node of the second case is the walking process of the horse tread board.

How can you find the walking path of this horse on the board by searching this tree of discovery? You can use a depth-first search method to access individual nodes in the tree in an orderly manner until you access the leaf nodes.

If the leaf node is the leaf node of the second case, then the search process can be completed, because the walking path of the horse stepping Board is found, and if the leaf node is the leaf node of the first case, then it is not able to go through, then it is necessary to return to the node of the previous layer and continue the deep first search along the next branch of

Therefore, in the design of the "Horse Stepping Board" algorithm, we can draw on the depth-first traversal algorithm and the two-fork tree of the first order traversal algorithm. But there is no need to really build such a tree of discovery, only to borrow the idea of exploring the tree.

In the actual operation process, the so-called exploration tree is actually the exploration path of depth-first search, each node is actually the current chessboard state, and the so-called leaf node or in the current chessboard state, the "horse" can no longer walk next, or the Horse board success.

Complete program

Note: Because the program involves deep search, running the middle node can no longer derive from other "walking" branch, that is, the "horse" does not go through, then return to the previous node, more complex, slow to run. (It usually takes about a minute for the result to appear, please be patient.) )

#include <stdio.h>#defineX 8#defineY 8intChess[x][y];intNextxy (int*x,int(ynintCount/*find the next available location based on the x, y location*/{    Switch(count) { Case 0:            if(*x+2<=x-1&& *y-1>=0&& chess[*x+2][*y-1]==0)            {                *x=*x+2; *y=*y-1; return 1; }             Break;  Case 1:            if(*x+2<=x-1&& *y+1<=y-1&& chess[*x+2][*y+1]==0)            {                *x=*x+2; *y=*y+1; return 1; }             Break;  Case 2:            if(*x+1<=x-1&& *y-2>=0&& chess[*x+1][*y-2]==0)            {                *x=*x+1; *y=*y-2; return 1; }             Break;  Case 3:            if(*x+1<=x-1&& *y+2<=y-1&& chess[*x+1][*y+2]==0)            {                *x=*x+1; *y=*y+2; return 1; }             Break;  Case 4:            if(*x-2>=0&& *y-1>=0&& chess[*x-2][*y-1]==0)            {                *x=*x-2; *y=*y-1; return 1; }             Break;  Case 5:            if(*x-2>=0&& *y+1<=y-1&& chess[*x-2][*y+1]==0)            {                *x=*x-2; *y=*y+1; return 1; }             Break;  Case 6:            if(*x-1>=0&& *y-2>=0&& chess[*x-1][*y-2]==0)            {                *x=*x-1; *y=*y-2; return 1; }             Break;  Case 7:            if(*x-1>=0&& *y+2<=y-1&& chess[*x-1][*y+2]==0)            {                *x=*x-1; *y=*y+2; return 1; }             Break; default:             Break; }    return 0;}intTravelchessboard (intXintYintTag/*Depth First search ground "horse Tread Board"*/{    intX1=x, Y1=y, flag=0, count=0; Chess[x][y]=tag; if(Tag = = x*Y) {return 1; } Flag=nextxy (&x1, &Y1, Count);  while(flag==0&& count<7) {Count=count+1; Flag=nextxy (&x1, &Y1, Count); }     while(flag) {if(Travelchessboard (x1, y1, tag+1))            return 1; X1=x; Y1=y; Count=count+1; Flag=nextxy (&x1, &y1, Count);/*looking for the next (x, y)*/         while(flag==0&& count<7)        {  /*Loop to find the next (x, y)*/Count=count+1; Flag=nextxy (&x1, &Y1, Count); }    }    if(Flag = =0) Chess[x][y]=0; return 0;}intMain () {intI, J;  for(i=0; i<x; i++)         for(j=0; j<y; J + +) Chess[i][j]=0; if(Travelchessboard (2,0,1))    {         for(i=0; i<x; i++)        {             for(j=0; j<y; J + +) printf ("%-5d", Chess[i][j]); printf ("\ n"); } printf ("The horse has travelled the chess borad\n"); }    Elseprintf ("The horse cannot travel the chess board\n"); return 0;}

Operation Result:

-------   -   1-----   34   2 (   5    12)--   9    of  3    7 (  6)   4   8   27The Horse has travelled the chess Borad

"Data structure and algorithm analysis-C language Realization" horseshoe 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.