These little programs are here to show examples of usage of backtracking. There are classical problems which can be easily resolved with backtracking.
The "model algorithm" for backtracking is the following, according to Wikipedia :
Function Backtracking (candidate : data)
- if reject(candidate), i.e candidate can't be a solution, return false.
- if accept(candidate), i.e candidate can be a solution, or is a solution, returns true.
- nextCandidate = generates first candidate.
- while size of solution is not reached, do
- if Backtracking(nextCandidate), returns true.
- otherwise, nextCandidate = next candidate to test.
- enjoy :)
This version finds just one solution to the problem. If you want to show all solutions (which can have no sense, for instance for Sudoku problems which have a priori only one solution), you just have to replace 4.1) by Backtracking(nextCandidate).