#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
int deltai[]={2,1,-1,-2,-2,-1,1,2};
int deltaj[]={1,2,2,1,-1,-2,-2,-1};
int board[8][8];
int exitn(int i, int j, int s, int a[]){
int i1,j1,k,count;
for(count=k=0;k<8;k++){
i1=i+deltai[(s+k)%8];
j1=j+deltaj[(s+k)%8];
if(i1>=0 && i1<8 && j1>=0 && j1<8 && board[i1][j1]==0)
a[count++]=(s+k)%8;
}
return count;
}
int next(int i, int j, int s){
int m,k,kk,min,a[8],b[8],temp;
m=exitn(i,j,s,a);
if(m==0) return -1;
for(min=9,k=0;k<m;k++){
temp=exitn(i+deltai[a[k]],j+deltaj[a[k]],s,b);
if(temp<min){
min=temp;
kk=a[k];
}
}
return kk;
}
void main(){
int sx,sy,i,j,step,no,start;
for(sx=0;sx<8;sx++)
for(sy=0;sy<8;sy++){
start=0;
do{
for(i=0;i<8;i++)
for(j=0;j<8;j++)
board[i][j]=0;
board[sx][sy]=1;
i=sx;
j=sy;
for(step=2;step<=64;step++){
if((no=next(i,j,start)) == -1) break;
i+=deltai[no];
j+=deltaj[no];
board[i][j]=step;
}
if(step>64) break;
start++;
}while(step<=64);
for(i=0;i<8;i++){
for(j=0;j<8;j++)
printf("%4d",board[i][j]);
printf("\n\n");
}
printf("if you want to find another way,input enter!\n");
scanf("%c");
}
}