这是我写的骑士之旅的程序,执行是对的,但我总觉得有点问题,但是不知怎么解决
#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;
for(i=0;i<8;i++)
{
for (j=0;j<8;j++)
printf("%3d",chess[i][j]);
printf("\n");
}
printf("\n");
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");
}
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;
}