1

What is the most efficient way to calculate the number of possible unique routes a knight can take, given the start coordinate, end coordinate, an infinite chessboard, and the length of the route has to be n steps?

What I currently have is not fast enough according to a speed test.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int checkColor(int a, int b) {
  if ((((a % 2 == 0) || (a == 0)) && ((b % 2 == 0) || (b == 0))) || a == b) {
    return 0;
  }
  return 1;
}
int checkInsuficientDistance(int n, int xb, int yb, int xe, int ye) {
  return 5 * n * n < (pow((xe - xb), 2) + pow((ye - yb), 2));
}
int numRoutes(int xb, int yb, int xe, int ye, int n) {
  if(n == 0){
    if(!(xe == xb && ye == yb)){
      return 0;
    }else{
      return 1;
    }
  }   
  if (checkInsuficientDistance(n, xb, yb, xe, ye)) {
    return 0;
  }
  int totalRoutes = 0;
  totalRoutes += numRoutes(xb + 2, yb + 1, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb + 2, yb - 1, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb - 2, yb + 1, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb - 2, yb - 1, xe, ye, n - 1 );

  totalRoutes += numRoutes(xb + 1, yb + 2, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb - 1, yb + 2, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb + 1, yb - 2, xe, ye, n - 1 );
  totalRoutes += numRoutes(xb - 1, yb - 2, xe, ye, n - 1 );
  return totalRoutes;
}

int main(int argc, char* argv[]) {
  int xb, yb, xe, ye, length;
  printf("Start coordinate: ");
  scanf("%d %d", &xb, &yb);

  printf("End coordinate: ");
  scanf("%d %d", &xe, &ye);

  printf("Length: ");
  scanf("%d", &length);

  if ((checkColor(xb, yb) == checkColor(xe, ye)) && length % 2 != 0) {
    printf("\nNumber of routes: 0\n");
  }else{
    printf("\nNumber of routes: %d\n", numRoutes(xb, yb, xe, ye, length));
  }
  return 0;
}

0 Answers0