Below is a solution for the N-queens problem using the Python-Constraint Resolver from Labix. Could someone explain to me, or refer me to any web page where it explains the meaning of the last 3 lines of this code?
Moreover, how could I use the AllDifferentConstraint
constraint to make the below code shorter?
from constraint import *
problem = Problem()
size = 8
cols = range(size)
rows = range(size)
problem.addVariables(cols, rows)
for col1 in cols:
for col2 in cols:
if col1 < col2:
problem.addConstraint(lambda row1, row2, col1=col1, col2=col2:
abs(row1-row2) != abs(col1-col2) and
row1 != row2, (col1, col2))