The realization of C language by recursive + backtracking method of horse stepping checkerboard algorithm

Source: Internet
Author: User

R is the row of the Matrix, and C is the column of the matrix

Output the results to the results.txt in the current directory (need to be built in advance).

The results are given: 1. The existence of a path allows the horse to travel all squares as required;

2. Total number of solutions;

3. Time of execution of the program;

#include <stdio.h>#include<stdlib.h>#include<time.h>#defineR2#defineC 4intflag[r][c]={0};//a two-dimensional array that holds the horse jumping path    intarr[r][c]={0}; intx[8]={2,1,-1,-2,2,1,-1,-2}; inty[8]={1,2,2,1,-1,-2,-2,-1}; intn=1, Count; FILE*p; //initializing the file output stream    intinitout () {FP=fopen ("Results.txt","W");//Remember to close        if(fp==NULL) {printf ("File cannot open!" ); Exit (0); }        return 0; }        //Add a judgment function to determine if such a Hamiltonian path exists    voidjudgeexistence () {if(count==0) printf ("a chessboard of%d *%d does not exist that will allow the horse to traverse the path of each of the squares without repeating \ n", R,c); }    //output Horse Jump step stepout ()    voidStepOut () { for(intA=0; a<r;a++){                  for(intb=0; b<c;b++) {fprintf (FP),"%3d", Arr[a][b]); } fprintf (FP,"\ n"); } fprintf (FP,"\ n"); }        voidDFS (intIintj) {if(n==r*c) {count++; StepOut ();//write a function, output horse jump step stepout ()        }                Else           for(intk=0;k<8; k++)            {              if(i+x[k]>=0&&i+x[k]<r&&j+y[k]>=0&&j+y[k]<c&&flag[i+x[k]][j+y[k]]==0) {Flag[i+x[k]][j+y[k]]=1; n++; Arr[i+x[k]][j+y[k]]=n;//matrix assignment to record horse jumping stepsDFS (i+x[k],j+y[k]);//Loop + recursionFlag[i+x[k]][j+y[k]]=0; n--; }          }    }      voidMain () {clock_t start, finish; //How long does it take to calculate the program?        Longduration; Start=clock (); Count=0; intx, y; label_1:printf ("Please enter the horse's initial horizontal axis (x<=%d): x=\n", R); scanf ("%d",&X); if(x>R) {printf ("Please enter a number less than or equal to%d \ n", R); GotoLabel_1; } label_2:printf ("Please enter the horse initial ordinate (y<=%d): y=\n", c); scanf ("%d",&Y); if(y>B) {printf ("Please enter a number less than or equal to%d \ n", c); Gotolabel_2; } X=x-1; Y=y-1; Flag[x][y]=1; Arr[x][y]=1;        Initout ();        DFS (x, y);        Judgeexistence (); fprintf (FP,"The total number of solutions is:%d\n", Count); Finish=clock (); Duration=finish-start;//program execution time, per millisecondfprintf (FP,"the execution time of the program is:%10LD ms\n", duration);     Fclose (FP); }

What is wrong in the code welcome you to correct me.

The realization of C language by recursive + backtracking method of horse stepping checkerboard algorithm

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.