Advertisement
metalni

VI Zadaci za vezbanje prv kolokvium - Queens

Jun 30th, 2021
1,384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. from constraint import *
  2.  
  3. if __name__ == '__main__':
  4.     problem = Problem(BacktrackingSolver())
  5.     N = int(input())
  6.     variables = range(1, N + 1)
  7.     domain = [(i, j) for i in range(N) for j in range(N)]
  8.  
  9.     problem.addVariables(variables, domain)
  10.  
  11.     # ---Tuka dodadete gi ogranichuvanjata----------------
  12.     # x1 != x2
  13.     # y1 != y2
  14.     # abs(x1 - x2) != abs(y1 - y2)
  15.  
  16.     for q1 in variables:
  17.         for q2 in variables:
  18.             if q1 < q2:
  19.                 problem.addConstraint(
  20.                     lambda a, b: a[0] != b[0] and a[1] != b[1] and abs(a[0] - b[0]) != abs(a[1] - b[1]), (q1, q2))
  21.  
  22.     # ----------------------------------------------------
  23.  
  24.     if N <= 6:
  25.         print(len(problem.getSolutions()))
  26.     else:
  27.         print(problem.getSolution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement