I'm trying to write a javascript function that finds all possible routes of length N on a board (a 15x15 grid) where the player cannot move diagonally. I was able to come up with a pretty simple recursive solution but I suspect it is extremely unoptimized.
Here is the code:
search(n, u, v) {
if (n == 0 || isWall(u, v))
return;
board[v][u] = 2;
search(n - 1, u, v - 1);
search(n - 1, u + 1, v);
search(n - 1, u, v + 1);
search(n - 1, u - 1, v);
return;
}
board is a 2d array that contains the board's data. Free spaces, walls and reachable spaces are represented by 0s, 1s and 2s respectively.
Here's an example of what is looks like given N=6

EDIT: As mentionned below, I'm trying to find all reachable cells in N or less moves.