[英]Boolean expression for modified Queens problem
I saw the boolean expressions for the N Queens problem from here .我从这里看到了 N Queens 问题的 boolean 表达式。
My modified N queens rules are simpler:我修改后的 N 皇后规则更简单:
For ap*p chessboard I want to place N queens in such a way so that对于 ap*p 棋盘,我想以这样的方式放置 N 个皇后
For example, say N = 17, then we need a 5*5 chessboard and the placement will be:例如,假设 N = 17,那么我们需要一个 5*5 的棋盘,放置位置为:
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_*_*_*
*_*_*_*_*
The question is I am trying to come up with a boolean expression for this problem .问题是我正试图为这个问题想出一个 boolean 表达式。
This problem can be solved using the Python packages humanize
and omega
.这个问题可以使用 Python 包
humanize
和omega
来解决。
"""Solve variable size square fitting."""
import humanize
from omega.symbolic.fol import Context
def pick_chessboard(q):
ctx = Context()
# compute size of chessboard
#
# picking a domain for `p`
# requires partially solving the
# problem of computing `p`
ctx.declare(p=(0, q))
s = f'''
(p * p >= {q}) # chessboard fits the queens, and
/\ ((p - 1) * (p - 1) < {q}) # is the smallest such board
'''
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u)) # assert unique solution
p = d['p']
print(f'chessboard size: {p}')
# compute number of full rows
ctx.declare(x=(0, p))
s = f'x = {q} / {p}' # integer division
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u))
r = d['x']
print(f'{r} rows are full')
# compute number of queens on the last row
s = f'x = {q} % {p}' # modulo
u = ctx.add_expr(s)
d, = list(ctx.pick_iter(u))
n = d['x']
k = r + 1
kword = humanize.ordinal(k)
print(f'{n} queens on the {kword} row')
if __name__ == '__main__':
q = 10 # number of queens
pick_chessboard(q)
Representing multiplication (and integer division and modulo) with binary decision diagrams has complexity exponential in the number of variables, as proved in: https://doi.org/10.1109/12.73590用二元决策图表示乘法(以及 integer 除法和模)的复杂度在变量数量上呈指数增长,如以下所示: https://doi.org/10.1109/12.73590
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.