Horse Tread Board problem-greed (matlab&c++)

Source: Internet
Author: User
Tags exit in

1. Description of the problem

The horse is randomly placed in a square of a chess board[0~7][0~7], and the horse moves in accordance with the moves rules, all 64 squares on the board. The preparation of non-recursive procedures, to find the walking route, and according to the walking route, the digital 1,2,...,64 sequentially filled in an 8x8 square, output.

2.matlab Code

Clear Allclcchessboard=zeros (8);% initialization dirx=[2 1-1-2-2-1 1 2];% direction vector diry=[1 2 2 1-1 -2-2 -1];stack (1). X=0;stack (1). y=0;h Orse (Chessboard,dirx,diry,stack)
main.m
 function Horse (chessboard,dirx,diry,stack)% Program entry X=1;%x,y used to represent the coordinates of the initial position y=1;step=1;%step indicates the number of steps to walk init (); Chessboard (    x, y) =step;% The first step% applies the greedy algorithm to solve the for step=2:64 I=mix (X,y,dirx,diry,chessboard), the direction of the exit can go from a certain point, the direction of the minimum number of exports x=x+dirx (i);    Y=y+diry (i);    Stack (step). X=x;    Stack (step). Y=y; Chessboard (x, y) =step;endprint (chessboard,stack);% print result function init (chessboard)% initialize the chessboard to initialize all the squares to 0 chessboard= Zeros (8); function Mixdir=mix (x,y,dirx,diry,chessboard)% The input parameter is the coordinates of a point, and functions return the direction from which all the feasible directions from that point, the lowest number of exits mixdir=0;% Mixdir records the direction of the minimum number of exits Mixnumber=9;%mixnumber records the number of exits in that direction, that is, the minimum number of exits in all directions for I=1:8 if isOK (X+dirx (i), Y+diry (i), chessboard) t        Emp=outnumber (X+dirx (i), Y+diry (i), dirx,diry,chessboard); If temp && temp=1 && x<=8 && y>=1 && y<=8 && chessboard (x, y) ==0 result =1;else result=0;endfunction print (chessboard,stack)% print Data disp (chessboard) for i=1:64 fprintf (' Step%d, x=%d,y=%d\n ', I, Stack (i). X,stack (i). Y) End 
HORSE.M

3.c++ Code

#include #include #include using namespace std;/*--------------------------------                           The definition of-----chessboard---------------------------------------------*/int dirx[]={2,1,-1,-2,-2,-1,1,2};                           The array sequentially records eight moving directions of the horizontal axis int diry[]={1,2,2,1,-1,-2,-2,-1};                                                   The array sequentially records eight directional ordinate int chessboard[8][8]; Defines a 8*8 board/*--------------------------------------The progressive function of the horse--------------------------------------------*/void                                                 Init ();                                  Initialization, the main point is to place the chessboard 0 int outnumber (int m,int n);                                        In order to start from a certain point, there can be a number of paths to go int mix (int m,int n);                                                Seeking a direction, starting from that direction, the point of arrival, the number of paths that can be left is the minimum void print ();                                      Print Checkerboard result bool isOK (int m,int n); Determine if a direction is feasible/*--------------------------------------main function--------------------------------------------*/void Main () {cout<< "****************************** Horse Tread Board *********************************";            int x=0,y=0,step=1,i=0;                             X, y is used to represent the coordinates of the initial position, step indicates the number of steps to walk, and I is a generation with variable char ch;                              Determine if the input coordinates are correct init ();                 The user's input process chessboard[x][y]=step; Record the initial position, define the coordinates of the point as the number of steps step for (step=2;step<65;step++) {//Apply greedy algorithm to solve, no backtracking process i=mix (                        x, y);                        In the direction from which a certain point of departure can go, the direction of the smallest number of exits x+=dirx[i];   Former further y+=diry[i];  Chessboard[x][y]=step;                         Print ();                               Print the checkerboard results for each step,} print (); After 64 steps, using the greedy algorithm will have a solution, so no backtracking, direct printing results//exit this program} VOID init ()//Initialize the board, will all   The lattice is initialized to zero {for (int i=0;i<8;i++) for (int j=0;j<8;j++) chessboard[i][j]=0;          } int Mix (int m,int n)//The input parameter is the coordinate of a point, and the function returns the direction of the minimum number of exits in all feasible directions starting at that point {                                        Number of exits for a certain number of directions to go int mixdir=0,mixnumber=9,a=0;                                          Mixdir records the direction of the minimum number of exits, Mixnumber records the number of exits in that direction, that is, the minimum number of exits in all directions for (int i=0;i<8;i++) {if (isOK ((M+dirx[i]), (n+diry[i))) {  First to determine whether a direction is feasible a=outnumber ((m+dirx[i)), (N+diry[i]); Calculate the direction of exit if (a&& (A<mixnumber)) {//If the direction is feasible and the number of exits in that direction is smaller than the known minimum number of exits Mixnu                       Mber=a;                          Change the Mixnumber to the number of mixdir=i for that export; Record this direction as the minimum exit direction}}} if (mixdir==0) {//This step is for the last step, when step=63, due to exit in all directions   The number is zero, so special consideration is needed for the for (int i=0;i<8;i++) if (isOK ((m+dirx[i), (N+diry[i]))) return i;                               } return Mixdir;                                 Returns the direction of the minimum number of exits} int outnumber (int m,int n)//function returns the number of all feasible directions from that point for the incoming coordinates, i.e. the number of exits {int flag=0; Flag record direction number for (int i=0;i<8;i++)//EightThe direction is traversed once if (isOK (M+dirx[i]), (n+diry[i))) flag++;                            If a direction is feasible, the number of exits is +1 return flag; Returns the number of exits} bool isOK (int m,int n)//Determines if the point has passed, that is, whether a certain direction is feasible, feasible to return 1, otherwise return 0 {if ((m>=0) &AMP;&A                              MP, (m<8) && (n>=0) && (n<8) && (chessboard[m][n]==0)) return 1;   Without the edge of the chessboard, and without passing, that is, the feasible else return 0; } void Print ()//To print the information for the board, and to display the step information in the full grid {for (int i=0;i<8;i++) {cout< <endl; cout<<endl;   for (int j=0;j<8;j++) {cout.width (6);   COUT&LT;&LT;CHESSBOARD[I][J];} } cout<<endl;cout<<endl;    }
View Code

Horse Tread Board problem-greed (matlab&c++)

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.