Exemplo n.º 1
0
def main():
    m = matrix()

    print "Example covering:"
    # Take the first result from the iterator.
    solution = exactcover.Coverings(m).next()
    pprint.pprint(solution)
    print
    print solution_str(solution)
    print

    # Count the number of results returned by the iterator.
    print "There are {0} unique tilings.".format(
        sum(1 for x in exactcover.Coverings(m)))
Exemplo n.º 2
0
    def load_matrix(self, matrix, secondary=0):
        """
        Convert the input `matrix` into a form compatible with the
        `exactcover` C extension and load it into an `exactcover.Coverings`
        object.

        The input `matrix` is a two-dimensional list of tuples:

        * Each row is a tuple of equal length.

        * The first row contains the column names: first the puzzle piece
          names, then the solution space coordinates.  For example::

              ('A', 'B', 'C', '0,0', '1,0', '0,1', '1,1')

        * The subsequent rows consist of 1 & 0 (True & False) values.  Each
          row contains a 1/True value in the column identifying the piece, and
          1/True values in each column identifying the position.  There must
          be one row for each possible position of each puzzle piece.

        The `secondary` parameter is the number of secondary (rightmost)
        columns: columns which may, but need not, participate in the solution.

        The converted data structure consists of a list of lists of column
        names.
        """
        if self.solver:
            self._num_previous_searches += self.solver.num_searches
        rows = [sorted(item for item in row if item) for row in matrix[1:]]
        self.solver = exactcover.Coverings(rows,
                                           headers=matrix[0],
                                           secondary=secondary)
Exemplo n.º 3
0
def n_queens(n):
    matrix = []
    for row in range(n):
        for col in range(n):
            matrix.append(("r%d" % row, "c%d" % col, "d%d" % (row + col),
                           "a%d" % (row + n - 1 - col)))

    # The diagonal and antidiagonal constraints are secondary,
    # since not every diagonal will have a queen on it.
    secondary = set(("d%d" % i for i in range(2 * n - 1)))
    secondary.update(set(("a%d" % i for i in range(2 * n - 1))))

    solution = exactcover.Coverings(matrix, secondary).next()
    print_solution(n, solution)
Exemplo n.º 4
0
def sudoku(puzzle):
    """Solve a sudoku puzzle."""
    print "Solving puzzle:"
    print '\n'.join(puzzle.strip().split())
    print

    m = sudoku_matrix(sample_puzzle)
    solution = exactcover.Coverings(m).next()

    print "Solution partition:"
    pprint.pprint(solution)
    print
    print "Solved puzzle:"
    print solution_str(solution)
Exemplo n.º 5
0
        yield p


def squash(p, q):
    if p is None: return q
    if p.endswith(q): return ""
    for i in range(1, len(q)):
        if (p + q[-i:]).endswith(q):
            return q[-i:]


def solution_as_superpermutation(n, solution):
    s = []
    p = None
    for q in permutations_in_solution(n, solution):
        s.append(squash(p, q))
        p = q
    return "".join(s)


n = int(sys.argv[1])
# matrix, secondary = double_3cycle(n)
matrix, secondary = single_4cycle(n)
for solution in exactcover.Coverings(matrix, secondary):
    try:
        print solution_as_superpermutation(n, solution)
    except BadSolution:
        print >> sys.stderr, "Ignoring bad solution"
        pass
# for row in solution: print row