示例#1
0
def _sudoku_solver_1(_sudoku):
    sudoku = [row[:] for row in _sudoku]
    
    # Get all the unknown tiles (positions with x)
    free_positions = []
    for i in xrange(9):
        for j in xrange(9):
            if sudoku[i][j] == 'x':
                free_positions.append((i,j))
    if len(free_positions) == 0:
        raise StopIteration
     
    # Build the list of generators to get all the possible
    # answers for the current sudoku           
    possible_values = []
    for pos in free_positions:
        possible_values.append(get_possible_values_for_a_given_position(
                                    sudoku, pos[0], pos[1]))
    
    # Create the generator
    answer_generator = itertools.product(*possible_values)
    
    # Check if the answer can be used to create a valid sudoku
    # and yield it
    for answer in answer_generator:
        for pos, value in enumerate(answer):
            i,j = free_positions[pos]
            sudoku[i][j] = value
        # Check if the given answer is valid           
        if is_a_valid_sudoku(sudoku):
            yield sudoku
                
    raise StopIteration
                count += 1

            if count != 1:
                sudoku[i][j] = value
                tiles.append(tile)
                random.shuffle(tiles)
                c += 1
                if c == len(tiles):
                    break
            else:
                # print "Remaining tiles: ", empty_tiles
                empty_tiles -= 1
                c = 0

        e_t = 0
        for i in xrange(9):
            for j in xrange(9):
                if sudoku[i][j] == "x":
                    e_t += 1

        if e_t != EMPTY_TILES:
            raise ValueError("Expecting", EMPTY_TILES, "free tiles, got", e_t)

        for c, s in enumerate(sudoku_solver(sudoku)):
            if not is_a_valid_sudoku(s):
                raise ValueError("The solved sudoku is not valid")
            if c > 0:
                raise ValueError("The generated board has more than one solution")

    print REPS, "valid boards generated"
from SudokuTools import is_a_valid_sudoku

def generate_sudoku_board():
    return fill_partial_sudoku(generate_partial_sudoku())           
             
if __name__ == '__main__':
    # Using the custom algorithim
    reps = 7500;
    sudokus = []
    
    start = time.time()
    for v in xrange(reps):
        sudoku = None
        count, sudoku = generate_sudoku_board()

        if sudoku == None or not is_a_valid_sudoku(sudoku):
            print sudoku
            raise ValueError("Invalid sudoku generated")
        sudokus.append(sudoku)
    end = time.time()
    
    sudokus.sort()
    for x in xrange(reps-1):
        if sudokus[x] == sudokus[x+1]: 
            raise ValueError("Two equal sudokus generated")
    
    print reps, "valid and different sudokus generated in", end - start, "secs using the custom algorithm"
    
    # Using the solver to generate the sudokus
    sudoku = [ ['x' for _ in xrange(9)] for _ in xrange(9)]
    g = sudoku_solver(sudoku)