The code is as follows:
#include <stdio.h>#include<time.h>#defineX 5#defineY 5intChess[x][y];voidprintchess () {inti,j; printf ("This is horse chess:\n"); for(i=0; i<x;i++){ for(j=0; j<y;j++) {printf ("%2d\t", Chess[i][j]); } printf ("\ n"); }}intNextint*x,int*y,intStep) { Switch(step) { Case 0: if(*y+2<=y-1&& *x-1>=0&& chess[*x-1][*y+2]==0) { *y+=2; *x-=1; return 1; } Break; Case 1: if(*y+2<=y-1&& *x+1<=x-1&& chess[*x+1][*y+2]==0) { *y+=2; *x+=1; return 1; } Break; Case 2: if(*y+1<=y-1&& *x+2<=x-1&& chess[*x+2][*y+1]==0) { *y+=1; *x+=2; return 1; } Break; Case 3: if(*y-1>=0&& *x+2<=x-1&& chess[*x+2][*y-1]==0) { *y-=1; *x+=2; return 1; } Break; Case 4: if(*y-2>=0&& *x+1<=x-1&& chess[*x+1][*y-2]==0) { *y-=2; *x+=1; return 1; } Break; Case 5: if(*y-2>=0&& *x-1>=0&& chess[*x-1][*y-2]==0) { *y-=2; *x-=1; return 1; } Break; Case 6: if(*y-1>=0&& *x-2>=0&& chess[*x-2][*y-1]==0) { *y-=1; *x-=2; return 1; } Break; Case 7: if(*y+1<=y-1&& *x-2>=0&& chess[*x-2][*y+1]==0) { *y+=1; *x-=2; return 1; } Break; default: Break; } return 0;}intHorseintXintYinttag) { intx_t=x,y_t=y; intflag=0, count=0; Chess[x][y]=tag; if(tag==x*Y) {printchess (); return 1; } Flag=next (&x_t,&y_t,count); while(!flag && count<=7) {Count++; Flag=next (&x_t,&y_t,count); } while(flag) {if(Horse (x_t,y_t,tag+1)) return 1; x_t=x,y_t=y,count++; Flag=next (&x_t,&y_t,count); while(!flag && count<=7) {Count++; Flag=next (&x_t,&y_t,count); } } if(!flag) 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; }} clock_t Begin,end; Begin=clock (); if(!horse (2,0,1) {printf ("The horse Chess is unavailable!"); } End=clock (); printf ("This time used is%lf\n",(Double) (End-begin)/clocks_per_sec); return 0;}
Run:
Soft-shelled turtle data structure and algorithm-horse stepping board (Knight travel problem)