1

i am new to programing an trying to learn it. As I am always looking for new intressting python tutorials i have come accross an tutorial for a sudoku solver using backtracking technique.

https://www.techwithtim.net/tutorials/python-programming/sudoku-solver-backtracking/

Since this sudoku solver only shows one single solution, I was wondering if there was a way to set a count on how many solutions of a sudoku can be found. Or how I can modify the code so that it doesn't stop after finding a solution but keeps on looking for other possible solutions. I don't know how i can get it to backtrack again to find other solutions. I was already looking this problem up on the internet but most of the solutions i found didn't really fit to my problem. If someone has an idea on how to do this I would be really happy about any help i can get, since i am worrying about this problem for a long time now. Already thank you for helping me, looking forward to maybe being able to solving my problem:)

board = [
    [7,8,0,4,0,0,1,2,0],
    [6,0,0,0,7,5,0,0,9],
    [0,0,0,6,0,1,0,7,8],
    [0,0,7,0,4,0,2,6,0],
    [0,0,1,0,5,0,9,3,0],
    [9,0,4,0,6,0,0,0,5],
    [0,7,0,3,0,0,0,1,2],
    [1,2,0,0,0,7,4,0,0],
    [0,4,9,2,0,6,0,0,7]
]


def solve(bo):
    find = find_empty(bo)
    if not find:
        return True
    else:
        row, col = find

    for num in range(1,10):
        if valid(bo, num, (row, col)):
            bo[row][col] = num          

            if solve(bo):                 
                return True

            bo[row][col] = 0              

    return False


def valid(bo, num, pos):
    # Check row
    for field in range(len(bo[0])):                     
        if bo[pos[0]][field] == num and pos[1] != field:
            return False

    # Check column
    for line in range(len(bo)):
        if bo[line][pos[1]] == num and pos[0] != line:
            return False

    # Check box
    box_x = pos[1] // 3
    box_y = pos[0] // 3

    for i in range(box_y*3, box_y*3 + 3):
        for j in range(box_x * 3, box_x*3 + 3):
            if bo[i][j] == num and (i,j) != pos:
                return False

    return True


def print_board(bo):
    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(bo[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j]) + " ", end="")


def find_empty(bo):
    for i in range(len(bo)):
        for j in range(len(bo[0])):
            if bo[i][j] == 0:
                return (i, j)  # row, col

    return None
if __name__ == "__main__":
    print_board(board)
    solve(board)
    print("___________________")
    print("")
    print_board(board)
Lisa09
  • 11
  • 2
  • I know that i somehow need to get my program to do the backtracking process again, so that it does't stop after one found solution but trys to find other solutions. So far everything I have tried didn't work at all so I would be happy about every tip I can get:) – Lisa09 Jan 20 '22 at 16:18
  • What have you tried so far, and could you post the code that you copied into the body of the post? There multiple code snippets on the page. – BrokenBenchmark Jan 20 '22 at 16:19
  • I have tried to store my solution, so that I can go on again with the backtracking. But then I still just got one and the same solution (I modified the soduku thats now in the code so that there is an sudoku that has two solutions) – Lisa09 Jan 20 '22 at 16:37
  • Have a look at [this answer](https://stackoverflow.com/a/56581709/5237560). It contains a sudoku solver (named shortSudokuSolve) that works as a generator and could give you some inspiration on how to approach it – Alain T. Jan 20 '22 at 17:23

0 Answers0