递归求骑士之旅问题,结果正确,却有问题,求救!
#include <stdio.h>
int step(int,int);
int next[2][8]={{2,1,-1,-2,-2,-1,1,2},{-1,-2,-2,-1,1,2,2,1}};
int chess[8][8]={0};
int counter;
main()
{
int i,j,x,y;
counter=1;
printf("Please input the initial location of the house(0--8)\n");
scanf("%d,%d",&x,&y);
chess[x][y]=1;
step(x,y);
for(i=0;i<8;i++)
{
for (j=0;j<8;j++)
printf("%3d",chess[i][j]);
printf("\n");
}
}
int step(int startx,int starty)
{
int i,nextx,nexty;
for(i=0;i<=7;i++)
{
nextx=startx+next[0][i];
nexty=starty+next[1][i];
if (isValid(nextx,nexty)==1)
{
counter++;
chess[nextx][nexty]=counter;
step(nextx,nexty);
}
}
}
int isValid(int x,int y)
{
if ((x>=0 && x<=7 && y>=0 && y<=7) && (chess[x][y]==0))
return 1;
return 0;
}