I am using following code in Codeblocks IDE in C, trying to solve the Knight's tour problem using recursion and backtracking. But the thing is it goes on forever and doesn't give any output, though I think it is not a case of Infinite recursion.
#include <stdio.h>
#include <conio.h>
int board[8][8]= {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}};
int i = 1;
int next(int a, int b);
int main()
{
int j, k;
next(0,0);
for(k = 0; k < 64; k++)
{
printf(" %d ", board[k/8][k%8]);
if((i+1)%8==0)
printf("\n");
}
}
int next(int a, int b)
{
if(i==64)
{
board[a][b]=64;
return 1;
}
if((a<0||a>7||b<0||b>7))
return 0;
if(board[a][b]!=0)
return 0;
printf(" %d %d ", a, b);
//getch();
board[a][b]= i;
if(next(a+2, b+1))
{
i++;
return 1;
}
if(next(a+1, b+2))
{
i++;
return 1;
}
if(next(a-1, b+2))
{
i++;
return 1;
}
if(next(a+2, b-1))
{
i++;
return 1;
}
if(next(a-2, b-1))
{
i++;
return 1;
}
if(next(a-1, b-2))
{
i++;
return 1;
}
if(next(a+1, b-2))
{
i++;
return 1;
}
if(next(a-2, b+1))
{
i++;
return 1;
}
board[a][b]=0;
return 0;
}