Written interview of backtracking algorithm

First, what is backtracking?

Backtracking, by definition, when the problem solution can not, back up, select a path again solved. That is when the big problem, we often will a big problem into a number of small problems, but every little problem when there are multiple solution (to have wrong) time (similar binary tree), you need to Solution one by one test, select the answers in line with established rules. After then backtracking is when selecting a solution and the solution does not comply with the rules of the solution, can be traced back to the previous step, choose another one an idea of ​​the solution algorithm.

Second, the specific legend

For example, the following figure, the male Whenever there is a small green hair have to fall back to the previous step to select a path, until rescued care of the household!
Here Insert Picture Description

Third, the spatial arrangement of the tree structure of the subset of the tree and Solutions

  1. Subset of the tree
    when given problem is to find from the set S n elements satisfying certain properties of the subset, the corresponding solution space becomes a subset of the tree.
    The 0-1 knapsack problem, several items from the selection into the backpack weight to the different values of the items, so that the backpack is not satisfied in the case of overweight, the maximum value of items within a backpack. Its solution space is a typical subset of the tree. (Only put part)
  2. Tree arrangement
    when given problem is to determine n elements arranged to meet some properties, the solution space is arranged in a respective tree.
    Such as the traveling salesman problem, a salesman traveling to several cities again, require a minimum walk away. Its solution is to arrange several cities, the solution space is arranged in a tree. (To all)

Fourth, the backtracking algorithm constructs routines

1 solution space defined problem

2 Determine easily searchable solution space structure

3 in a depth-first search the solution space, and a function to prevent invalid pruning search using the search process.

Fifth, practice :( selected from Leetcode exam)

N represents the number of generation is given in parentheses, you write a function, it is possible to generate all possible combinations of brackets and effective.

For example, given n = 3, to generate the result is:

[
"((()))", "(()
())",
"(()) ()",
"() (())",
"() () ()"
]
Link: https: //leetcode-cn.com/problems/generate-parentheses

If the subject process the use of violence, need to select all the possible and selected according to the rule matching the rule traversal solutions, i.e. the time complexity is (2 ^ (2n) n) , there is a certain amount of code amount. Well, because the problem also involves a variety of solution, and to choose a solution to meet the requirements, how if we use backtracking? (Recommended to try myself to write, and then look at the answer)
The answer is as follows:
the first layer of the answer ((()))
and then gradually return to function until the back number in parentheses ((
so the answer to the second layer (() ())
and then gradually return to function until the back number in parentheses (()
so the answer to the third layer (()) ()
and then gradually return to function until you return to the number of brackets (
it is the answer to the fourth floor () (() )
and then gradually return to function until the back number in parentheses () (
it is the answer to the fifth floor () () ()

class Solution {
    public List<String> generateParenthesis(int n) {
       List<String> lst = new ArrayList();
       trackback(lst,"",0,0,n);
       return lst; 

    }

    public void trackback(List<String> lst,String curr,int open,int close,int max){
        //当括号数等于给定括号数的两倍(左右括号),添加进容器
        if(curr.length()==max*2){
            lst.add(curr);
            return ;
        }
        //当左括号小于给定的括号数,随便加
        if(open<max){
            trackback(lst,curr+"(",open+1,close,max);
        }
        //当右括号小于左括号数,随便加
        if(close<open){
            trackback(lst,curr+")",open,close+1,max);
        }
    }
}

Exercise Two: Eight Queens problem

Published 24 original articles · won praise 0 · Views 588

Guess you like

Origin blog.csdn.net/weixin_43896829/article/details/104892436