#include<stdio.h>
int qipan[8][8];
int x1,y1;
int x2,y2;
int check1 [2] ;
int k,n,m;
int check2 [2];
int count1 =1;
int count = 1;
int check(int,int);
int main ()
{
int c,z=0,x,y,l;
check1[0] = -1;
check1[1] = -1;
for(c=0;c<8;c++)
{
for(z=0;z<8;z++)
{
qipan[c][z] = 0;
}
}
printf("请选择你的起始位置:");
scanf("%d %d",&x,&y);
l = qipan[x][y] = count1++;
check(x,y);
for(c=0;c<8;c++)
{
for(z=0;z<8;z++)
{
printf("%2d ",qipan[c][z]);
}
printf("\n");
}
return 0;
}
int check(int x,int y)
{
int i=0,j=0;
int p1[8][2] = {{x-2,y-1},{x-2,y+1},{x-1,y+2},{x-1,y-2},{x+1,y-2},{x+1,y+2},{x+2,y-1},{x+2,y+1}};
int (*p2)[2] =p1;
for(i=0;i<8;i++)
{
for(j=0;j<2;j++)
{
if(*(*(p2+i)+j)>=0&&*(*(p2+i)+j)<8)
{
k = check1[j] = *(*(p2+i)+j);
}
}
if(check1[0] == -1 ||check1[1] == -1)
{
check1[0] = check1[1] = -1;
}
if(check1[0]!=-1&&check1[1] !=-1)
{
if(qipan[check1[0]][check1[1]]==0)
{
qipan[check1[0]][check1[1]] = count1++;
x2 = check1[0];
y2 = check1[1];
check1[0] = check1[1] = -1;
count++;
if(count<64)
{
x1 = x;
y1 = y;
check(x2,y2);
}
else
{
return 1;
}
}
check1[0] = check1[1] = -1;
}
}
x = x1;
y = y1;
return 0;
}
|