ZebraTutor. N-Queens Problem Backtracking Demo. A visualization and explanation of how a backtracking algorithm searches for a solution to a problem. For performance reasons n is bound between 4 and 10. Note: Requires canvas supported browser. Tested on Chrome, Safari, Edge, Firefox. If I have a chessboard of width and length n where n is a number. How can n queens be placed so that none of the queens are attacking one another? Try it yourself here with 8 queens. What would the algorithm do without backtracking? Without backtracking a program would place all n queens before checking if any queens are attacking. We don’t need a whole row full of queens before we know that the solution failed. Introducing backtracking A backtracking algorithm is a way of computing a solution to a problem when constraints are known.
The algorithm places one queen at a time. If you want to see this in action. For more detail check out the wiki page on backtracking. CSP Backtracking. CHR Online Examples. Alexander-Schiendorfer/cp-examples.