I've been trying to improve my recursion skills in C and I came across this question. I've tried to solve it, yet the code doesn't seem to work properly. For example, there are 108 options for the knight to move from (1,1) to (8,8) in 6 moves and in my code the result is completely different. The question asks how many ways are there to move a knight from (1,1) to (8,8) in n moves(n given from the user) in 8x8 board. here's my code:
#include <stdio.h>
#define SIZE 8
//x,y coordinates of the knight.
int knightsTour(int x, int y, int num);
void main() {
int n;
int result;
do {
scanf(" %d", &n);
result = knightsTour(1,1,n);
printf("%d\n", result);
} while (n > 0);
}
int knightsTour(int x,int y,int num) {
int result = 0;
int i, j;
if (num == 0) {
return 0;
}
if (((x > 8) || (y > 8))||((x == 8) && (y == 8))) {
return 0;
}
for (i = 1; i <= SIZE; i++) {
for (j = 1; j <= SIZE; j++) {
if ((i != y) && (j != x) && ((i != y + j) && (j != x + i)) && ((i != y + j) && (j != x - i))
&& ((i != y - j) && (j != x + i)) && ((i != y - j) && (j != x - i))) {
result += knightsTour(i, j, num - 1) + 1;
}
}
}
return result;
}
Your code has several problems:
One approach would be the following recursive method:
Putting this together:
#include <stdio.h>
#define SIZE 8
int knightsTour(int x, int y, int num)
{
if (x < 1 || x > SIZE) return 0;
if (y < 1 || y > SIZE) return 0;
if (num == 0) return (x == SIZE && y == SIZE);
return knightsTour(x + 2, y + 1, num - 1)
+ knightsTour(x + 1, y + 2, num - 1)
+ knightsTour(x - 1, y + 2, num - 1)
+ knightsTour(x - 2, y + 1, num - 1)
+ knightsTour(x - 2, y - 1, num - 1)
+ knightsTour(x - 1, y - 2, num - 1)
+ knightsTour(x + 1, y - 2, num - 1)
+ knightsTour(x + 2, y - 1, num - 1);
}
int main(void)
{
int result = knightsTour(1, 1, 6);
printf("%d\n", result);
return 0;
}
This code is straightforward and it determines the 108 possible moves.