Queen N looks at the backtracking routine

This is a good way to talk about backtracking.

My initial thoughts
Taking 4 queens as an example, I draw a search tree, and the grids of the chessboard are all "." at the beginning.
insert image description here
For each row, select a grid and set it as "Q", and select one row at a time. There are four choices in the first row.
In order to avoid column conflicts when selecting the queen for the next row, there are three options.
Continue to choose to build a complete solution, and may encounter diagonal conflicts.
In case of conflict, it is meaningless to continue to choose, and no legal solution can be obtained. Backtracking is required.

backtracking routine

  • Iterate through all possible options.
  • Try the choices in turn: make a choice, and recurse down.
  • If this choice does not produce the correct solution, undo the choice (reverting the current "Q" to "."), return to the previous state, and make the next available choice.
  • It is a process of selecting, exploring, and unselecting. When a dead end is identified, backtrack and try the next point without doing an ineffective search.

Thought correction
When enumerating options, I avoided conflicts between rows and columns, and the search tree drawn earlier has been pruned.

But I still think it's complicated. Diagonal conflicts and row-column conflicts should be used as constraints together, and full pruning can be done directly.

That is, to traverse the previous row, if the current grid is in the same column or diagonal as the previous queens, then skip this point (this needs to be optimized, which will be discussed later)

Look at the two leaf nodes on the left side of the picture above, no matter how you place the next line, it will conflict, and the optional options have been cut.

When all optional choices are iterated, the current recursive branch ends, and the last choice is undone, returning to the previous level, and cutting into another branch.

When the fourth line is filled in, as shown in the green hook in the above figure, a solution is generated, added to the solution set, and returned (it’s okay not to return here, because sufficient pruning has been done, and if you don’t return, you will go through iterations, and recursion is also end), start backtracking, and continue to search for a complete solution.
insert image description here

Three points of backtracking

The choice determines the search space and determines which nodes the search space has.
Constraints, used to prune branches to avoid entering invalid branches.
Goal, which determines when to capture a valid solution, end the recursion early, and start backtracking.

Author: xiao_ben_zhu
Link: https://leetcode-cn.com/problems/n-queens/solution/shou-hua-tu-jie-cong-jing-dian-de-nhuang-hou-wen-t/
Source: Force The copyright of LeetCode
belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.


Backtracking method:

  • Thought: Probe, try to do it first, and then go back if it doesn’t work
  • practice:
  1. Traverse and enumerate all possible options (the backtracking problem may seem a bit complicated at first glance, consider a simple situation, don't be stingy with drawing pictures, tree diagrams and the like to help clarify ideas.

The choice determines the search space and determines which nodes the search space has.

  1. Try these options in turn: (different questions, different selection methods, can be to modify the state, push into the array, exchange the order, mark, etc.)
  2. Make a selection and recurse down.

Constraints, used to prune branches to avoid entering invalid branches.

  1. If this choice does not produce the correct solution, undo the choice (reverting the current "Q" to "."), return to the previous state, and make the next available choice.

Goal, which determines when to capture a valid solution, end the recursion early, and start backtracking.

It is a process of selecting, exploring, and unselecting. When a dead end is identified, backtrack and try the next point without doing an ineffective search.

Different questions, possible choices, constraints, and three points of goals have changed

Guess you like

Origin blog.csdn.net/ClaireSy/article/details/114736388