Esempio n. 1
0
def queens_problem(n):
    board = make_board(n)
    return conjoin(
        flatten(exactly_one(row) for row in board),
        flatten(at_most_one(col) for col in transpose(board)),
        flatten(at_most_one(diag) for diag in rising_diagonals(board)),
        flatten(at_most_one(diag) for diag in falling_diagonals(board)))
Esempio n. 2
0
def queens_problem(n):
    board = make_board(n)
    return conjoin(
        flatten(exactly_one(row) for row in board),
        flatten(at_most_one(col) for col in transpose(board)),
        flatten(at_most_one(diag) for diag in rising_diagonals(board)),
        flatten(at_most_one(diag) for diag in falling_diagonals(board)))
Esempio n. 3
0
def make_sudoku_grid(n):
    problem = []
    def add_permutation(slots):
        for variables in transpose(slots):
            problem.extend(at_most_one(variables))

    make_variable = count(1).next
    def make_slot():
        slot = [make_variable() for i in range(n*n)]
        problem.extend(exactly_one(slot))
        return slot
    def make_block():
        block = [[make_slot() for c in range(n)] for r in range(n)]
        add_permutation(flatten(block))
        return block

    superblock = [[make_block() for c in range(n)] for r in range(n)]
    rows = flatten([map(flatten, transpose(row)) for row in superblock])

    for row in rows:
        add_permutation(row)
    for column in transpose(rows):
        add_permutation(column)

    return rows, problem
Esempio n. 4
0
def make_sudoku_grid(n):
    problem = []

    def add_permutation(slots):
        for variables in transpose(slots):
            problem.extend(at_most_one(variables))

    make_variable = count(1).next

    def make_slot():
        slot = [make_variable() for i in range(n * n)]
        problem.extend(exactly_one(slot))
        return slot

    def make_block():
        block = [[make_slot() for c in range(n)] for r in range(n)]
        add_permutation(flatten(block))
        return block

    superblock = [[make_block() for c in range(n)] for r in range(n)]
    rows = flatten([map(flatten, transpose(row)) for row in superblock])

    for row in rows:
        add_permutation(row)
    for column in transpose(rows):
        add_permutation(column)

    return rows, problem
Esempio n. 5
0
 def make_block():
     block = [[make_slot() for c in range(n)] for r in range(n)]
     add_permutation(flatten(block))
     return block
Esempio n. 6
0
 def make_block():
     block = [[make_slot() for c in range(n)] for r in range(n)]
     add_permutation(flatten(block))
     return block