Order the steps
Order the parts of a backtracking function.
- Check if the branch can be pruned
- Apply the choice, recurse, then undo it
- Loop over the choices for the next step
- Check if the path is complete and record a copy
Choose an answer, then open Show answer below to check it.
Show answer
Answer: D A C B
- Check if the path is complete and record a copy This is the base case.
- Check if the branch can be pruned Stop early when no answer can come from here.
- Loop over the choices for the next step Each choice is one child in the decision tree.
- Apply the choice, recurse, then undo it Undoing restores the state for the next choice.