Algorithm summary-backtracking

Recently, I have brushed a lot of backtracking topics on leetcode, and I have brushed all the questions that can be brushed on the leetcode backtracking topic . Now I will make a simple summary of the backtracking.

The backtracking algorithm is actually a search trial process similar to enumeration, which is mainly to find the solution of the problem in the search trial process. When it is found that the solution condition is not satisfied, it will "backtrack" and return and try another path. The backtracking method is a kind of optimal search method, which searches forward according to the optimal conditions to achieve the goal. But when you explore a certain step, you find that the original choice is not good or the goal is not achieved, so you will go back one step and choose again. This technique of going back and going again is the backtracking method, and the point of a certain state that satisfies the backtracking condition This is called the "backtrack point." Many complex and large-scale problems can use the backtracking method, which has the reputation of "universal problem-solving method". 

If you don't take its definition, I always feel that this article has some shortcomings, so I extracted the above paragraph from the backtracking algorithm of Baidu Encyclopedia . In layman's terms, backtracking is a search process that tries multiple possibilities. When the conditions are met, it keeps going, and when the conditions are not met, turn back and try other paths without hitting the south wall or looking back.

When to use backtracking?

When a problem requires multiple possible results, and at this time, even if it is impossible to write violently, please try backtracking, such as permutation and combination problems.

Backtracking code framework

The backtracking algorithm has a framework. First of all, it is a recursion and needs to set the recursive termination condition; find all possible paths in the next step in the recursive function, and then call each possible recursively, set before the call, and clear the setting after the call.

It is roughly as follows:

void backtrace() {     if (termination condition) {         processing result         return;     }     for (all possible next step) {         pruning         setting variable         backtrace(); // recursively         clearing variable setting // backtrace     } }










When there is only one possibility in the next step, it is pure recursion at this time, and there is no choice of a path to go on. At this time, there is no need to backtrack.

Pruning is to improve efficiency. Don't do it if you know it's impossible. For example, if the number 1 in the arrangement is already used, don't use it in the next step. It is also okay to consider pruning conditions in enumerating all possible processes.

What kind of problems can backtracking solve?

arrangement:

46. ​​Full arrangement : full arrangement without repeated numbers

47. Arrangement II : Arrangement with repeated numbers

Sword Finger Offer 38. Arrangement of Strings  &  Interview Questions 08.08. Arrangements and Combinations with Repeated Strings: Arrangement with Repeated Characters

Interview questions 08.07. Permutations and combinations without repeated strings

784. Alphabetical order in all upper and lower case

1079. Movable type printing : repeat all permutations of any character in a string

Interview Question 08.09. Brackets  &  22. Bracket generation : print all legal combinations of n pairs of brackets

 

combination:

77. Combination : return all possible combinations of k numbers in 1...n

216. Combination Sum III : Find all the combinations of k numbers that add up to n

39. Combination sum : the combination of the number of the target (the number can be reused)

40. Combination Sum II : The combination of the number with the target (the number cannot be reused)

17. Letter combination of phone number : numbers 2-9 correspond to 3-4 letters

1780. Determine whether a number can be expressed as a sum of powers of three : a combination of k numbers

1286. Letter combination iterator :

 

Child collection, division:

131. Split palindrome string : split into palindrome string

842. Split an array into Fibonacci sequences : Split a string of numbers into Fibonacci sequences

93. Restore IP address : restore a numeric string to an IP address

1593. Split the string to maximize the number of unique substrings :

78. Subsets  & interview questions 08.04. Power set : output all subsets

306. Cumulative number : similar to Fibonacci

 

path:

797. All possible paths :

980. Different Path III :

332. Re-arrange the itinerary :

 

checkerboard:

51. N Queen  &  Interview Questions 08.12. Eight Queens :

37. Solving Sudoku :

79. Word search :

Guess you like

Origin blog.csdn.net/hbuxiaoshe/article/details/115320295