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!
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."