コード例 #1
0
 def solve_recursively(sudoku_possible_values):
     # Check if we ran into a contradiction:
     if contradiction(sudoku_possible_values):
         return None;
     else:
         # Propagate
         propagate(sudoku_possible_values, k);
         # Check for contradictions
         if contradiction(sudoku_possible_values):
             return None;
         # Find a cell that is still uncertain
         uncertain_cell = find_uncertain_cell(sudoku_possible_values);
         if uncertain_cell == None:
             return sudoku_possible_values;
         else:
             i,j = uncertain_cell;
             # Recurse on the different values for cell i,j
             possibilities = sudoku_possible_values[i][j];
             for poss in possibilities:
                 sudoku_possible_values_copy = deep_copy(sudoku_possible_values);
                 sudoku_possible_values_copy[i][j] = [poss];
                 answer = solve_recursively(sudoku_possible_values_copy);
                 if answer != None:
                     return answer;
         # If no solution was found in the recursion, conclude there is no solution
         return None;
コード例 #2
0
    def solve_recursively(sudoku_possible_values):
        """
        This function first checks whether the sudoku_possible_values includes a contradiction,
        using the function contradiction()
        """

        # Check if we ran into a contradiction:
        """
        This function just checks if stuff is valid
        """
        if contradiction(sudoku_possible_values):
            return None
        else:
            # Propagate
            sudoku_possible_values = propagate(sudoku_possible_values, k)
            # Check for contradictions *again*
            if contradiction(sudoku_possible_values):
                return None
            # Find a cell that is still uncertain (?)
            """
            this function determines whether the possible values matrix still has entries with len > 1
            if this is not the case, and given that the matrix has no contradictions, we return the sudoku_possible_values
            otherwise, we recurse on the different values for cell i,j in the matrix
            """
            uncertain_cell = find_uncertain_cell(sudoku_possible_values)
            if uncertain_cell == None:
                return sudoku_possible_values
            else:
                i, j = uncertain_cell
                # Recurse on the different values for cell i,j
                possibilities = sudoku_possible_values[i][j]
                for poss in possibilities:
                    sudoku_possible_values_copy = deep_copy(
                        sudoku_possible_values)
                    sudoku_possible_values_copy[i][j] = [poss]
                    answer = solve_recursively(sudoku_possible_values_copy)
                    if answer != None:
                        return answer
            # If no solution was found in the recursion, conclude there is no solution
            return None