0

Is anyone able to find a mistake in my knight's tour code? I can't seem to find it, and I'm getting an infinite loop, not a stack overflow

private bool heuristic(int[,] board, int x, int y, ref int jmp)
{
    if (x < 0 || x > 7 || y < 0 || y > 7 || board[x, y] > 0)
        return false;
    board[x, y] = ++jmp;
    if (jmp == 64)
        return true;

    if (heuristic(board, x + 2, y + 1, ref jmp) ||
        heuristic(board, x + 2, y - 1, ref jmp) || heuristic(board, x - 2, y + 1, ref jmp) ||
        heuristic(board, x - 2, y - 1, ref jmp) || heuristic(board, x + 1, y + 2, ref jmp) ||
        heuristic(board, x + 1, y - 2, ref jmp) || heuristic(board, x - 1, y + 2, ref jmp) ||
        heuristic(board, x - 1, y - 2, ref jmp))
        return true;
    board[x, y] = 0;
    jmp--;
    return false;
}

And calling it:

var board = new int[8,8];
var x = 0;
var y = 0;
var jmp = 0;
var result = heuristic(board, x, y, ref jmp);

I need to have a jmp variable as I'm preforming multiple trials and also want to show the path taken. Thanks!

false
  • 10,264
  • 13
  • 101
  • 209
Stefan
  • 700
  • 10
  • 22
  • 1
    How sure are you that it is an infinite loop, and not just taking a very, very long time? – John Burger Jun 14 '16 at 02:35
  • @JohnBurger It's an 8x8 board. – Stefan Jun 14 '16 at 02:37
  • @Stefan That doesn't answer the question.. :) Can you please provide the starting conditions, so that people can reproduce the code? That is, the intial array, `x`, `y`, and `jmp`? – Rob Jun 14 '16 at 02:39
  • 1
    You've got eight recursive calls, each of which could go 63 deep. 8^63 is a very, very large number. I know that by no means every call will go that deep - but they start out with a huge number of options before getting "cornered" by previous visits. – John Burger Jun 14 '16 at 02:42
  • @Rob everything is 0, including the initialmatrix, filled with 0's – Stefan Jun 14 '16 at 02:45

1 Answers1

2

According to Wikipedia:

There are 26,534,728,821,064 [...] tours

and

A brute-force search for a knight's tour is impractical on all but the smallest boards; for example, on an 8x8 board there are approximately 4×1051 possible move sequences, and it is well beyond the capacity of modern computers (or networks of computers) to perform operations on such a large set. However, the size of this number gives a misleading impression of the difficulty of the problem, which can be solved "by using human insight and ingenuity ... without much difficulty."

John Burger
  • 3,662
  • 1
  • 13
  • 23
  • 1
    Divide and conquer? Warnsdorf's rule? Check the WIkipedia article I linked. There have been *PhD* s written on how best to do this... – John Burger Jun 14 '16 at 03:05
  • Warnsdorf's rule seems to be: for each possible next location, check how many places are reachable from *there* (in other words, only go two deep). Then choose the one with the *least* number of future options. If nothing else it'll prevent a full depth-first search... – John Burger Jun 14 '16 at 03:25
  • Thanks for pointing this out, I read about this on Wikipedia but for some reason I didn't expect that an 8x8 board would go that deep in DFS. I switched the recursive calls to do it in clockwise direction and it solves it quickly. I'll add the Warnsdorff's rule. – Stefan Jun 15 '16 at 16:43