马的遍历——这到底是怎么回事???
程序代码:#include <stdio.h>
#define N 8
int moveX[]={-2,-1,1,2,2,1,-1,-2};
int moveY[]={1,2,2,1,-1,-2,-2,-1};
int board[N][N];
int next(int r,int c,int array[N][N],int step)
{
int tempX,tempY,i;
if(step==N*N)
return 0;
step++;
for(i=0;i<8;i++)
{
tempX=r+moveX[i];
tempY=c+moveY[i];
if(tempX>=0 && tempX <N && tempY>=0 && tempY<N && array[tempX][tempY]==0)
{
array[tempX][tempY]=step;//--------
if(! next(tempX,tempY,array,step))
return 0;
}
}
return 1;
}
int main()
{
int startX,startY,i,j;
printf("input coordinate of the first node:x and y\n");
scanf("%d%d",&startX,&startY);
board[startX][startY]=1;
next(startX,startY,board,1);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%-4d",board[i][j]);
printf("\n\n");
}
return 0;
}希望大家帮我看一下,谢谢!
[ 本帖最后由 c4fun 于 2013-6-20 10:49 编辑 ]







