Consider the source board:
+---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+
When you first call your function, the algorithm puts the queen in column 0 and row 0 because you call it with col = 0 and because for (int i = 0; i < N; i++) starts at 0. Your board becomes
+---+---+---+---+---+---+---+---+ | Q | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+
Then you call the function recursively, with col = 1 , so you try to put the queen in col=1 and line=0 . You get impossible placement because queens can take each other, so you continue the for (int i = 0; i < N; i++) loop for (int i = 0; i < N; i++) and ultimately succeed in putting the queen in col=1 and line=2 , you will get this board:
+---+---+---+---+---+---+---+---+ | Q | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | Q | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+
Now you continue to do this, increasing col each time. In the end, you will be taken to this board:
+---+---+---+---+---+---+---+---+ | Q | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | Q | | | | | +---+---+---+---+---+---+---+---+ | | Q | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | Q | | | | +---+---+---+---+---+---+---+---+ | | | Q | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | Q | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | Q | | +---+---+---+---+---+---+---+---+
You have a problem because this board does not allow the queen's position in column 7. You will have to back off. It happens that a recursive function reaches return false; after all positions in the column have been taken and no position has been found. When the function returns false, the previous function call will resume execution on the line
if ( solveNQUtil(board, col + 1) == true )
Since the call returned true, the rest of the loop body will be executed, i will increase, and the algorithm will continue to search. Think of it as a giant set of nested loops:
for(int i = 0; i < 8; ++i) { for(int j = 0; j < 8; ++j) { //Snip 6 other fors board[0, i] = 1; board[1, j] = 1; //Snip if(isValid(board)) return board; //Snip clean up } }
What do you replace with recursive function calls. This illustrates that backtracking actually just allows the previous recursive level to move on to the next attempt. In this case, this means an attempt at a new position, while in other applications it will be an attempt at the next generated move.
I think you need to understand that the state of the previous recursive call is not lost when you call the same function again. When you reach the line
if ( solveNQUtil(board, col + 1) == true )
The state of the current function is still on the stack, and a new stack stack is created for a new call to solveNQUtil . When this function returns, the previous one can continue execution and, in this case, increase on which line he is trying to place the queen.
Hope this helps. The best way to wrap your head around this stuff is to reduce your problem to a smaller domain (say, fewer queens) and do it with a debugger.