1

我从这里看到了 N 皇后问题的布尔表达式。

我修改后的 N 皇后规则更简单:

对于 ap*p 棋盘,我想以这样的方式放置 N 个皇后,以便

  1. 皇后将被相邻放置,行将首先被填充。
  2. p*p 棋盘大小将被调整,直到它可以容纳 N 个皇后

例如,假设 N = 17,那么我们需要一个 5*5 的棋盘,其位置将是:

Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_Q_Q_Q
Q_Q_*_*_*
*_*_*_*_*

问题是我试图为这个问题想出一个布尔表达式

4

1 回答 1

1

这个问题可以使用 Python 包humanizeomega.

"""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

于 2020-09-10T01:02:09.693 回答