簡體   English   中英

Boolean 修正皇后區問題的表達式

[英]Boolean expression for modified Queens problem

我從這里看到了 N Queens 問題的 boolean 表達式。

我修改后的 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_*_*_*
*_*_*_*_*

問題是我正試圖為這個問題想出一個 boolean 表達式

這個問題可以使用 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)

用二元決策圖表示乘法(以及 integer 除法和模)的復雜度在變量數量上呈指數增長,如以下所示: https://doi.org/10.1109/12.73590

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM