Solves the Sudoku board via the backtracking algorithm
(board)
| 59 | |
| 60 | |
| 61 | def solve(board): |
| 62 | """Solves the Sudoku board via the backtracking algorithm""" |
| 63 | |
| 64 | empty = find_empty(board) |
| 65 | if not empty: # no empty spots are left so the board is solved |
| 66 | return True |
| 67 | |
| 68 | for nums in range(9): |
| 69 | if valid(board, empty, nums + 1): |
| 70 | board[empty[0]][empty[1]] = nums + 1 |
| 71 | |
| 72 | if solve(board): # recursive step |
| 73 | return True |
| 74 | board[empty[0]][empty[1]] = 0 # this number is wrong so we set it back to 0 |
| 75 | return False |
| 76 | |
| 77 | |
| 78 | if __name__ == "__main__": |
no test coverage detected