class SubsetSumSolver2: def __init__(self): self.enumerator = BacktrackingEnumerator(createSolution=lambda space, i, d, f: f) def solve(self, w, W): space = SubsetSumStateSpace2(w, W) return next(self.enumerator.enumerate(space))#]s2
class DLXSolver:#[solver def __init__(self): self.solver = BacktrackingEnumerator(createSolution=lambda space, i, d, f: f) def solve(self, ones): space = DLX0(ones) return(next(self.solver.enumerate(space)))#]solver
class NQueensDLXSolver: #[solver def __init__(self): self.solver = BacktrackingEnumerator() def solve(self, N: "int") -> "IList<int> or None": M, row_coding = self.nqueens_to_matrix(N) space = DLX2(M, set(range(2 * N))) self.solver.createSolution = \ lambda space, i, d, s: self.selected_nqueens(s.selected, row_coding) return next(self.solver.enumerate(space), None) def nqueens_to_matrix(self, N: "int") -> "matrix, IList<(int, int)>": M = [] rows_coding = [] for row in range(N): for col in range(N): M.append([ row, N + col, 2 * N + col - row + N - 1, 2 * N + 2 * N - 1 + col + row ]) rows_coding.append((row, col)) return M, rows_coding def selected_nqueens(self, selected: "IList<int>", row_coding: "IList<(int, int)>") \ -> "IList<int> or None": if selected != None: return tuple(q[1] for q in sorted(row_coding[row] for row in selected)) else: return None #]solver
class PerfectHashFinder: def __init__(self): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: f) def find(self, words, maxvalue): space = PerfectHashStateSpace(words, maxvalue) return next(self.enumerator.enumerate(space)) #]full
class HamiltonianCycleSolver: #[solver def __init__(self): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: f + (f[0], )) def solve(self, G): space = HamiltonianCycleStateSpace(G) return next(self.enumerator.enumerate(space)) #]solver
class ExactCoverSolver: def __init__(self): self.solver = BacktrackingEnumerator( createSolution=lambda space, i, d, f: d) def solve(self, matrix): space = ExactCoverStateSpace(matrix) return (next(self.solver.enumerate(space)))
class SubsetSumSolver3: def __init__(self): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: d + [0]*(space.n-len(d))) def solve(self, w, W): space = SubsetSumStateSpace3(w, W) return next(self.enumerator.enumerate(space))
class PolyominoesSolver: def __init__(self, ): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: f) def solve(self, board, tetrominos): space = PolyominoesStateSpace(board, tetrominos) return next(self.enumerator.enumerate(space))
class HamiltonianCycleSolver:#[solver def __init__(self): self.enumerator = BacktrackingEnumerator(createSolution =lambda space, i, d, f: f + (f[0],)) def solve(self, G): space = HamiltonianCycleStateSpace(G) return self.enumerator.enumerate(space) #]solver def find(self,S): sol=HamiltonianCycleSolver().solve(S) return sol
class WolfGoatCabbageSolver: def __init__(self): self.solver = BacktrackingEnumerator( createSet=lambda space: StateSet(), createSolution=lambda space, i, d, f: d) def solve(self) -> "IList<str>": space = WolfGoatCabbageSpaceState() return next(self.solver.enumerate(space)) #> full
class PolyominoesSolver: #[dlx def __init__(self): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: f) def solve(self, board, polyominoes): matrix, row_coding = self.polyominos_matrix_and_rows_coding( board, polyominoes) space = DLX(matrix) selected_rows = next(self.enumerator.enumerate(space)) return self.solution_as_string(selected_rows, board, row_coding) def polyominos_matrix_and_rows_coding(self, board, polyominoes): polyomino_column = {} col = 0 for polyomino in polyominoes: symbol, pieces = polyomino polyomino_column[symbol] = col col += 1 board = [list(row) for row in board.split('\n')] xy_column = {} for y in range(len(board)): for x in range(len(board[y])): if board[y][x] == '.': xy_column[x, y] = col col += 1 matrix = [] rows_coding = [] for polyomino in polyominoes: symbol, pieces = polyomino for piece in pieces: for y in range(len(board)): for x in range(len(board[y])): if all(0<=x+i<len(board[y]) and 0<=y+j<len(board) and \ board[y+j][x+i] == '.' for (i,j) in piece): rows_coding.append((symbol, piece, x, y)) matrix.append([polyomino_column[symbol]]) for (i, j) in piece: matrix[-1].append(xy_column[x + i, y + j]) return matrix, rows_coding def solution_as_string(self, selected_rows, board, rows_coding): board = [list(row) for row in board.split('\n')] for selected_row in selected_rows: symbol, piece, x, y = rows_coding[selected_row] for i, j in piece: board[y + j][x + i] = symbol return ('\n'.join(''.join(row) for row in board)).replace("*", " ") #]dlx
def __init__(self): self.enumerator = BacktrackingEnumerator(createSolution =lambda space, i, d, f: f + (f[0],))
def __init__(self): self.solver = BacktrackingEnumerator()
def __init__(self): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: d + [0]*(space.n-len(d)))
def __init__(self): self.solver = BacktrackingEnumerator( createSolution=lambda space, i, d, f: d)
def __init__(self, ): self.enumerator = BacktrackingEnumerator( createSolution=lambda space, i, d, f: f)
def match(self, x: "str") -> "bool": space = RegularExpressionMatchingStateSpace(self.nfa, x) return BacktrackingEnumerator().first(space) != None #]match