mirror of
https://git.freebsd.org/ports.git
synced 2025-06-14 09:10:32 -04:00
22 lines
666 B
Python
22 lines
666 B
Python
# example from https://ericpony.github.io/z3py-tutorial/guide-examples.htm
|
|
|
|
from z3 import Int, And, Distinct, If, solve, print_matrix
|
|
|
|
|
|
# We know each queen must be in a different row.
|
|
# So, we represent each queen by a single integer: the column position
|
|
Q = [ Int('Q_%i' % (i + 1)) for i in range(8) ]
|
|
|
|
# Each queen is in a column {1, ... 8 }
|
|
val_c = [ And(1 <= Q[i], Q[i] <= 8) for i in range(8) ]
|
|
|
|
# At most one queen per column
|
|
col_c = [ Distinct(Q) ]
|
|
|
|
# Diagonal constraint
|
|
diag_c = [ If(i == j,
|
|
True,
|
|
And(Q[i] - Q[j] != i - j, Q[i] - Q[j] != j - i))
|
|
for i in range(8) for j in range(i) ]
|
|
|
|
solve(val_c + col_c + diag_c)
|