EightQueens.java.html

  1  /**
  2     This class solves the eight queens problem using backtracking.
  3  */
  4  public class EightQueens
  5  {
  6     public static void main(String[] args)
  7     {
  8        solve(new PartialSolution(0));
  9     }
 10  
 11     /**
 12        Prints all solutions to the problem that can be extended from 
 13        a given partial solution.
 14        @param sol the partial solution
 15     */
 16     public static void solve(PartialSolution sol)
 17     {
 18        int exam = sol.examine();
 19        if (exam == PartialSolution.ACCEPT) 
 20        { 
 21           System.out.println(sol); 
 22        }
 23        else if (exam == PartialSolution.CONTINUE)
 24        {
 25           for (PartialSolution p : sol.extend())
 26           {
 27              solve(p);
 28           }
 29        }
 30     }
 31  }
 32