コード例 #1
0
ファイル: test_dlx.py プロジェクト: mharrys/sudoku
    def test_no_solution(self):

        def callback(solutions, s):
            self.assertTrue(False)

        problem = array([[]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0, 0],
                         [0, 0]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0, 1]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[1, 0, 0],
                         [0, 0, 0],
                         [0, 0, 1]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)
コード例 #2
0
ファイル: solveitup.py プロジェクト: Jacob33123/narorumo
def main():
    if(len(sys.argv) != 2):
        print("usage: python3 solveitup.py <problem description>")
        return

    numvars, clauses = getproblem(sys.argv[1])
    grid = buildgrid(numvars, clauses)

    dlx = DLX(SparseMatrix(grid))
    results = dlx.search()
    printresults(results)
コード例 #3
0
ファイル: test_dlx.py プロジェクト: mharrys/sudoku
    def test_problem_1(self):

        def callback(solutions, s):
            self.validate([1], solutions, s)

        problem = array([[1]])

        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)
コード例 #4
0
ファイル: test_dlx.py プロジェクト: ChristerNilsson/2020
    def test_problem_2(self):
        def callback(solutions, s):
            self.validate([1, 4, 5], solutions, s)

        problem = array([[0, 0, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 0, 1],
                         [0, 1, 1, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0],
                         [0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 1, 1, 0, 1]])

        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)
コード例 #5
0
    def __call__(self, sudoku):
        board, metadata = SudokuSolver.parse(sudoku)
        result = DLX.solve(board)
        if result is None:
            return None

        # Draw back the solution.
        solution = np.zeros((9, 9))
        for row in result:
            cell = metadata[row]
            solution[cell.row, cell.col] = cell.val
        return solution
コード例 #6
0
ファイル: sudoku.py プロジェクト: liujiantong/sudoku-solver
    def solve(self, line):
        """
        Return list of solutions from specified line.
        Return empty list if no solutions are found and return at most
        one solution if validation is enabled or all solutions if validation
        is disabled. It is possible for a Sudoku challenge to have more than
        one solution but such challenge is considered to be an invalid.
        """
        grid = self.build_challenge(line)

        dlx = DLX.from_sudoku(grid, self.result)
        dlx.run()

        return self.grids
コード例 #7
0
ファイル: test_dlx.py プロジェクト: ChristerNilsson/2020
    def test_no_solution(self):
        def callback(solutions, s):
            self.assertTrue(False)

        problem = array([[]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0, 0], [0, 0]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[0, 1]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)

        problem = array([[1, 0, 0], [0, 0, 0], [0, 0, 1]])
        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)
コード例 #8
0
ファイル: test_dlx.py プロジェクト: mharrys/sudoku
    def test_problem_3(self):

        def callback(solutions, s):
            self.validate([2, 4, 6], solutions, s)

        problem = array([[1, 0, 0, 1, 0, 0, 1],
                         [1, 0, 0, 1, 0, 0, 0],
                         [0, 0, 0, 1, 0, 0, 1],
                         [0, 0, 1, 0, 1, 1, 0],
                         [0, 1, 1, 0, 0, 1, 1],
                         [0, 1, 0, 0, 0, 0, 1]])

        dlx = DLX.from_matrix(problem, callback)
        dlx.run(True)
コード例 #9
0
ファイル: sudoku.py プロジェクト: mharrys/sudoku
    def solve(self, line):
        """Return list of solutions from specified line.

        Return empty list if no solutions are found and return at most
        one solution if validation is enabled or all solutions if validation
        is disabled. It is possible for a Sudoku challenge to have more than
        one solution but such challenge is concidered to be an invalid.

        """
        grid = self.build_challenge(line)
        self.validate_challenge(grid)

        self.grids = []

        dlx = DLX.from_sudoku(grid, self.result)
        dlx.run(self.validate)

        return self.grids
コード例 #10
0
ファイル: scott.py プロジェクト: fractalleaf/exact-cover
               X="#BB99DD", Y="#CC88CC", Z="#DD99BB")

# Grid for Scott's pentomino problem: 8x8 with a 2x2 hole in the middle
GRID = Grid((8, 8), holes=[(3, 3), (3, 4), (4, 3), (4, 4)])

# Generate all pentominoes that can be placed on the grid
POLYMINOES = generate_all(PENTOMINOES, GRID)

# convert to list of lists
POLYMINOES = [polymino.aslist for polymino in POLYMINOES]

# calculate labels for DLX 
LABELS = list(set([element for polymino in POLYMINOES for element in polymino]))
LABELS = sorted(LABELS, key=sortkey)

COVER = DLX(LABELS, POLYMINOES)

SOLUTIONS = []
for i, SOLUTION in enumerate(COVER.generate_solutions()):
    print(i)
    SOLUTIONS.append(Grid.from_DLX(SOLUTION))

DISTINCT_SOLUTIONS = unique_grids(SOLUTIONS)

solutions_svg(DISTINCT_SOLUTIONS, filename='scott_distinct_solutions1.svg',
              columns=13, colour=COLOURS.get)

# removing X pentominoes
POLYMINOES = [pol for pol in POLYMINOES if not (pol[0] == 'X' and pol[1] not in [(5, 3), (5, 2), (4, 2)])]

COVER = DLX(LABELS, POLYMINOES)