1

ここから N クイーン問題のブール式を見ました。

私の変更された N クイーン ルールはより単純です。

ap*p chessboard の場合、次のように 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

humanizeこの問題は、Python パッケージとを使用して解決できます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 で証明されているように、変数の数が指数関数的に複雑になります。

于 2020-09-10T01:02:09.693 に答える