constraintNQueens.frink

Download or view constraintNQueens.frink in plain text format


/** This is a tester for Frink's constraint solver for the n-queens problem. */

n = 10
if length[ARGS] > 0       // Parse command-line argument
   n = parseInt[ARGS@0]

solver = newJava["frink.constraint.ConstraintSolver"]
q = new array
for i = 0 to n-1
   q@i = solver.makeIntRangeVariable[0,n-1]

for i = 0 to n-2
{
   for j = i+1 to n-1
   {
                                     // Queens not on
      solver.notEqual[q@i, q@j, 0]   // ... the same line
      solver.notEqual[q@i, q@j, i-j] // ... the same left diagonal
      solver.notEqual[q@i, q@j, j-i] // ... the same right diagonal
   }
}

listener = newJava["frink.constraint.PrintingSolutionListener"]
//listener.setFirstOnly[true]  // Comment this in to find only one solution.
solver.solve[listener]


Download or view constraintNQueens.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20167 days, 1 hours, 1 minutes ago.