Understanding recursion and backtracking from the n-queens problem

One of the most common solution is to use a recursive n queen, however, solving n queens problem, the main function was often only one function call statement:

eight_queen(0,n);

Yes, only this one function will calculate the results of the Queens of n, is not feeling very magical?

That's the beauty of recursion.

In fact, carefully read the code, we will find that, during operation, this is not just a function call, but by this function continues to call itself, forming a loop, and then find the solution of the problem.

We take a look at the following code:

void eight_queen(int index,int n)//index是行,n表示要放n个皇后
{
    int loop;
    for (loop = 0; loop < n; loop++)
    {
        if (check_pos_valid(index, loop))
        {
            gEightQueen[index] = loop;
            if (n-1 == index)
            {
                gCount++;print(n);
                return;
            }
            eight_queen(index + 1,n);
        }
    }
}

n = 4 specific call process is as follows:

gEightqueen [0] = 0, gEightqueen [1] = 2, gEightQueen [2] = no solution, a step returns back to continue checking the value of the loop;

                 gEightQueen [1] = 3, gEightQueen [2] = 2, gEightQueen [3] = no solution, continue to check back one step behind the loop value;

                                  gEightQueen [2] = no solution, gEightQueen [1] = 0 returns the step to continue checking the following values ​​loop;

                 gEightQueen [1] = no solution, a step returns back to continue checking the value of the loop, there is no return;

gEightQueen [0] = 1, gEightQueen [1] = 3, gEightQueen [2] = 0, gEightQueen [3] = 2, index = n-1, return; // return here is that the index is a function of the n-1 also call other functions;

                                  gEightQueen [2] = no solution, a step returns back to continue checking the value of the loop;

                 gEightQueen [1] = no solution, continue to check back one step behind the loop value;

gEightQueen [0] = 2, gEightQueen [1] = 0, gEightQueen [2] = 3, gEightQueen [3] = 1, index = n-1, return; // return the index here is to make the function of n-1 also call other functions;

gEightQueen [0] = 3, gEightQueen [1] = 0, gEightQueen [2] = no solution, continue to check back one step behind the loop value;

                 gEightQueen [1] = 1, gEightQueen [2] = no solution, continue to check back one step behind the loop value;

                 gEightQueen [1] = no solution, continue to check back one step behind the loop value;

gEightQueen [0] = no solution, the overall function call ends;

Summarize the recursive ideas:

         Suppose function eight_queen (index, n) is a, the function eight_queen (index, n) is b.

         When the function is called a loop current execution has not been completed, if there is a situation you can call a function in another function of b, b will be called first, but when the function b is finished, shall continue performance of a function not been executed, the most the worst case, all the functions are in all cases is performed again, the complexity of the algorithm is O (n ^ n), but in all cases a function to execute the function can not satisfy the condition b, b function will not be executed, so the actual call process complexity is often less than O (n * n)

        Procedure involves the use of recursion backtracking often, obtained through all possible solutions of the process.

 

Guess you like

Origin www.cnblogs.com/xbnl-bk-zm-2018/p/11422235.html