我的马踏棋盘

作者:袖梨 2022-07-02

/* 马踏棋盘                       */
/* horse2.c                           */
/* 2003-9-16                          */
/* 为马儿寻找一条不回溯的道路遍历棋盘 */
#include
#include
#include
#define N 6
int  v[]={-1,1,-2,2,-2,2,-1,1};
int  h[]={-2,-2,-1,-1,1,1,2,2};
int count=0;
int nl=0,nk=0,ndirec=0;
int nosolve=0;
struct square{
    int x;
    int y;
    int direc;
}a[N*N];
int board[N][N]={0};
int valid(int i,int j);
void jump(int i,int j,int d);
void outroute(FILE* fp);
void cartoon();
void main(){
 int x,y;
 int l,k,direc;
 int gdriver=DETECT,gmode;
 initgraph(&gdriver,gmode,"c:tc20bgi");
 printf("nnPlease input start position,range: (1,1)~(6,6)n");
 scanf("%d,%d",&l,&k);
 l--;k--;
 direc=0;
 count=0;
 setcolor(WHITE);
 while(1){
  jump(k,l,direc);
  if(nosolve){break;}
  l=nl;
  k=nk;
  direc=ndirec;
 }
 closegraph();
 getch();
}
/*  跳到棋盘上某一格  */
void jump(int k,int l,int direc){
int i,j;
  a[count].x=l;
  a[count].y=k;
  a[count].direc=direc;
  board[k][l]=count+1;
  if(count>=N*N-1){
     /* outroute(fp); */
     cartoon();
     count++;
     return;
  }
  else {
        count++;
        nl=l+v[direc];
        nk=k+h[direc];
        if(valid(nk,nl)){
            ndirec=0;
   &nbs

相关文章

精彩推荐