我正在尝试编写骑士巡回递归算法:
int mov[8][2] = {{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{-1,2},{1,-2},{-1,-2}};
typedef struct element {
int x;
int y;
struct element *prev, *next;
} node;
//adding pool to list
void dEnd(node **root, int x,int y)
{
node *pos;
pos = *root;
while(pos->next!= NULL)
pos = pos->next;
pos->next = (node *)malloc(sizeof(node));
pos->next->x=x;
pos->next->y=y;
pos->next->prev=pos;
pos->next->next=NULL;
}
void uEnd(node **root,int x,int y)
{
node *pos;
pos = *root;
while(pos->x!= x && pos->y !=y)
{
pos = pos->next;
}
pos->prev->next=NULL;
free(pos);
}
void printAll(node **root)
{
node *pos = *root;
while(pos->next)
{
printf("%d %d\n", pos->x,pos->y);
pos = pos->next;
}
}
int contains(int x,int y)
{
return(((x >= 0 ) && (x <= 7)) && ((y >= 0) && (y <= 7)));
}
//find move
int searchForMove(int x, int y, int **tab, node **list, int *number)
{
int i ;
for(i = 0; i < 8; i++)
{
int nx, ny;
nx = x + mov[i][0];
ny = y + mov[i][1];
if(contains(nx, ny) && !tab[nx][ny])
{
dEnd(list, nx, ny);
tab[nx][ny] = 1;
*number++;
if(!searchForMove(nx,ny,tab,list,number))
{
uEnd(list,nx,ny);
tab[nx][ny]=0;
*number--;
}
}
}
if(i == 7 && *number <64)
return 0;
if(*number == 64)
return 1;
}
有人可以告诉我我在哪里犯了错误吗?我已经逐步检查了哪些池算法添加到列表中。添加 4,3 池然后 6,4 池后什么是大惊喜算法应该以 6,4 作为实际位置调用它自己,但我不知道为什么它以 4,3 作为实际位置调用它自己。