我从这里中看到了N皇后问题的布尔表达式。
我修改的N皇后规则更简单:
对于一个p*p棋盘,我想用这样的方式放置N个皇后,以便
例如,假设N= 17,那么我们需要一个5*5的棋盘,其位置将是:
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_*_*_*
*_*_*_*_*问题是,我正试图为这个问题找到一个布尔表达式,。
发布于 2020-09-10 01:02:09
这个问题可以使用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)用二进制决策图表示乘法(以及整数除法和模)在变量数上具有指数复杂性,如以下所示:https://doi.org/10.1109/12.73590
https://stackoverflow.com/questions/63499032
复制相似问题