I am new to this site, but is there anyone out there who could point me in a different direction. I am trying to get the "Knights Tour" to work. The issue that I am having is that it seems to print several number twice.

I didn't test it, but here are the problems I saw:

First, your bounds check is incorrect - if((row == size) || (row < 0) || (column > size) || (column < 0)) should be if((row >= size) || (row < 0) || (column >= size) || (column < 0)) Second, you aren't backtracking at all - you're just picking a single path and following it as far as possible. To correct that you have to replace all the [B]else[/B] if s in the "tries each possible move" block with just if s, and if all of them fail, return the current position's state to unused (0). In pseudocode that would be something like this:

function next_move(x, y, level)
    board[x][y] = level
    if(level == side*side)
        print solution
        return found
    
    for each possible move (u,v) from (x,y)
        next_move(u, v, level+1)

    board[x][y] = 0

Then just call next_move(startx, starty, 1) from the main procedure.


And last, using simple backtracking results in an exponential algorithm that will take ages even for the current maximum you've set (20). You should consider using Warnsdorf's heuristic (or simply put, the next square you choose is the one that has the least possible moves left) which will bring down the complexity to linear in the total number of squares.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.