c# - Knight's Tour recursion -
is able find mistake in knight's tour code
? can't seem find it, , i'm getting infinite loop, not 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 have jmp
variable i'm preforming multiple trials , want show path taken. thanks!
according wikipedia:
there 26,534,728,821,064 [...] tours
and
a brute-force search knight's tour impractical on smallest boards; example, on 8x8 board there approximately 4×1051 possible move sequences, , beyond capacity of modern computers (or networks of computers) perform operations on such large set. however, size of number gives misleading impression of difficulty of problem, can solved "by using human insight , ingenuity ... without difficulty."
Comments
Post a Comment