c# - Knight's Tour algorithm -
c# - Knight's Tour algorithm -
i new programming , want resolve knight's tour practice. don't understand error on programme here method:
class knight { int n, m; bool[,] board ; public knight() { n = 5; board = new bool[n, n]; n *= n; m = n - 1; } public bool movement(int y, int x, int mov) { if (x < 0 || x >= 5 || y < 0 || y >= 5) { homecoming false; } if (board[x, y] == true) // has been in position { homecoming false ; } if (mov == m) { console.writeline("solution"); board[x, y] = true; homecoming true; } else { bool result = false; result = result || movement( x+2, y+1, mov+1); result = result || movement( x+2, y-1, mov+1); result = result || movement( x-2, y+1, mov+1); result = result || movement( x-2, y-1, mov+1); result = result || movement( x+1, y+2, mov+1); result = result || movement( x+1, y-2, mov+1); result = result || movement( x-1, y+2, mov+1); result = result || movement( x-1, y-2, mov+1); if (result == true) { console.writeline("{0} {1}", x, y); homecoming true; } else { board[x, y] = false; homecoming false; } } }
i not know why i'm getting repeated coordinates here output:
2 1 3 1 3 4 2 4 2 1 3 1 3 4 2 4 2 1 3 1 3 4 2 4 2 1 3 1 3 4 2 4 2 1 3 1 3 4 2 2 4 1 3 3 1 2 0 0
i think not setting
board[x, y] = true;
in else case, in case
if (result == true)
c#
Comments
Post a Comment