Exemple #1
0
def populateBoard(s, row, col):
    """
    Starting with a 9x9 grid of 0's, this function recursively populates
    the grid. It makes a list of integers from 1-9, shuffles the order, and
    tries the first number in the list in the current cell. If the inserted
    integer works, then it continues on. If the integer does not work then
    it tries the next one in the list. If none of the integers work, then
    set it to blank and return false.
    """
    if row == 8 and col == 8:
        used = pySudoku.test_cell(s, row, col)
        s[row][col] = used.index(0)
        return True

    if col == 9:
        row = row+1
        col = 0

    temp = list(range(1, 10))
    random.shuffle(temp)
    # Fill Sudoku
    for i in range(9):
        s[row][col] = temp[i]
        if checkValid(s, row, col):
            if populateBoard(s, row, col+1):
                return True
    s[row][col] = 0
    return False
Exemple #2
0
def DFS_solve(copy_s, row, col):
    #	DFS algorithm, while returning the number of solutions found.
    num_solutions = 0

    # Reached the last cells without no error, so there is a solution
    if row == 8 and col == 8:
        return num_solutions + 1

    if col == 9:
        row = row + 1
        col = 0

    if copy_s[row][col] == 0:
        used = pySudoku.test_cell(copy_s, row, col)
        # No possible solutions. Return 0 for number of solutions
        if 0 not in used:
            return 0

        while 0 in used:
            copy_s[row][col] = used.index(0)
            used[used.index(0)] = 1
            num_solutions += DFS_solve(copy_s, row, col + 1)

        # For No solution
        copy_s[row][col] = 0
        return num_solutions

    num_solutions += DFS_solve(copy_s, row, col + 1)
    return num_solutions
Exemple #3
0
def populateBoard(s, row, col):
    """
    Starting with a 9x9 grid of 0's, this function recursively populates
    the grid. It makes a list of integers from 1-9, shuffles the order, and
    tries the first number in the list in the current cell. If the inserted
    integer works, then it continues on. If the integer does not work then
    it tries the next one in the list. If none of the integers work, then
    set it to blank and return false.
    """
    if row == 8 and col == 8:
        used = pySudoku.test_cell(s, row, col)
        s[row][col] = used.index(0)
        return True

    if col == 9:
        row = row+1
        col = 0

    temp = list(range(1, 10))
    random.shuffle(temp)
    # Fill Sudoku
    for i in range(9):
        s[row][col] = temp[i]
        if checkValid(s, row, col):
            if populateBoard(s, row, col+1):
                return True
    s[row][col] = 0
    return False
Exemple #4
0
def DFS_solve(copy_s, row, col):
    """
    Recursively solves the copy_s puzzle with a backtracking
    DFS algorithm, while returning the number of solutions found.
    Starts at row 0 and column 0, and continues on to the right and
    down the rows.
    """
    num_solutions = 0

    # Reached the last cells without any error, so there is a solution
    if row == 8 and col == 8:
        return num_solutions + 1

    if col == 9:
        row = row+1
        col = 0

    if copy_s[row][col] == 0:
        # Used = list of size 10 representing which numbers are possible
        # in the puzzle. 0 = possible, 1 = not possible. Ignore index 0.
        used = pySudoku.test_cell(copy_s, row, col)
        # No possible solutions. Return 0 for number of solutions
        if 0 not in used:
            return 0

        while 0 in used:
            copy_s[row][col] = used.index(0)
            used[used.index(0)] = 1
            num_solutions += DFS_solve(copy_s, row, col+1)

        # Reached here? Then we tried 1-9 without success
        copy_s[row][col] = 0
        return num_solutions

    num_solutions += DFS_solve(copy_s, row, col+1)
    return num_solutions
Exemple #5
0
def DFS_solve(copy_s, row, col):
    """
    Recursively solves the copy_s puzzle with a backtracking
    DFS algorithm, while returning the number of solutions found.
    Starts at row 0 and column 0, and continues on to the right and
    down the rows.
    """
    num_solutions = 0

    # Reached the last cells without any error, so there is a solution
    if row == 8 and col == 8:
        return num_solutions + 1

    if col == 9:
        row = row+1
        col = 0

    if copy_s[row][col] == 0:
        # Used = list of size 10 representing which numbers are possible
        # in the puzzle. 0 = possible, 1 = not possible. Ignore index 0.
        used = pySudoku.test_cell(copy_s, row, col)
        # No possible solutions. Return 0 for number of solutions
        if 0 not in used:
            return 0

        while 0 in used:
            copy_s[row][col] = used.index(0)
            used[used.index(0)] = 1
            num_solutions += DFS_solve(copy_s, row, col+1)

        # Reached here? Then we tried 1-9 without success
        copy_s[row][col] = 0
        return num_solutions

    num_solutions += DFS_solve(copy_s, row, col+1)
    return num_solutions