|
| 1 | +def is_valid(board, row, col, num): |
| 2 | + """ Checks if the given number is valid for the given cell in the Sudoku board """ |
| 3 | + for i in range(9): |
| 4 | + if ( |
| 5 | + # checking is there any same number in row |
| 6 | + board[row][i] == num or |
| 7 | + # checking is there any same number in column |
| 8 | + board[i][col] == num or |
| 9 | + # checking is there any same number in cell(3*3) |
| 10 | + board[(row // 3) * 3 + i // 3][(col // 3) * 3 + i % 3] == num |
| 11 | + ): |
| 12 | + # if so return false |
| 13 | + return False |
| 14 | + return True |
| 15 | + |
| 16 | + |
| 17 | +"""Taking the Board and checking the availability""" |
| 18 | + |
| 19 | + |
| 20 | +def solve_sudoku(board): |
| 21 | + # use of recursive function solve. |
| 22 | + def solve(row, col): |
| 23 | + |
| 24 | + # if row number is 9, return true as there is no any other row (last Row Solved) |
| 25 | + if row == 9: |
| 26 | + return True |
| 27 | + |
| 28 | + # if column number is 9, then we should change the row and check again |
| 29 | + if col == 9: |
| 30 | + # resetting row number to next and column number to 0 |
| 31 | + return solve(row + 1, 0) |
| 32 | + |
| 33 | + # if the number of the board[row][col] is not the 0, then it is solved, then we should move o next. |
| 34 | + if board[row][col] != 0: |
| 35 | + # moving to next column |
| 36 | + return solve(row, col + 1) |
| 37 | + |
| 38 | + # setting a number between 1-9 |
| 39 | + for num in range(1, 10): |
| 40 | + |
| 41 | + # checking the availability of that number |
| 42 | + if is_valid(board, row, col, num): |
| 43 | + |
| 44 | + # if the number is ok,then setting the number |
| 45 | + board[row][col] = num |
| 46 | + |
| 47 | + # moving to next column |
| 48 | + if solve(row, col + 1): |
| 49 | + return True |
| 50 | + |
| 51 | + # if none above are not fitting, lets hold the value of cell as zero, and then we can call it again |
| 52 | + board[row][col] = 0 |
| 53 | + |
| 54 | + # returning none as algorithm cannot find any solution |
| 55 | + return False |
| 56 | + |
| 57 | + # starting with (0,0) cell |
| 58 | + if solve(0, 0): |
| 59 | + |
| 60 | + # return the solution |
| 61 | + return board |
| 62 | + else: |
| 63 | + |
| 64 | + # no solution |
| 65 | + return None |
| 66 | + |
| 67 | + |
| 68 | +""" printing the board """ |
| 69 | + |
| 70 | + |
| 71 | +def print_board(board): |
| 72 | + if board: |
| 73 | + |
| 74 | + # printing the board row by row |
| 75 | + for row in board: |
| 76 | + |
| 77 | + # joining the int as strings by including space in between |
| 78 | + print(" ".join(map(str, row))) |
| 79 | + else: |
| 80 | + |
| 81 | + print("No solution found.") |
| 82 | + |
| 83 | + |
| 84 | +def main(): |
| 85 | + |
| 86 | + board = [ |
| 87 | + [0, 0, 3, 0, 0, 0, 0, 0, 0], |
| 88 | + [0, 4, 0, 2, 0, 0, 6, 3, 0], |
| 89 | + [0, 8, 2, 1, 0, 0, 0, 0, 9], |
| 90 | + [0, 0, 0, 4, 0, 6, 0, 0, 0], |
| 91 | + [5, 0, 0, 0, 9, 0, 1, 0, 0], |
| 92 | + [3, 0, 0, 0, 0, 0, 0, 0, 0], |
| 93 | + [0, 0, 0, 0, 0, 0, 7, 8, 0], |
| 94 | + [0, 0, 0, 9, 0, 0, 0, 6, 0], |
| 95 | + [0, 0, 1, 7, 2, 0, 4, 0, 0] |
| 96 | + ] |
| 97 | + |
| 98 | + # taking the solution board |
| 99 | + solution = solve_sudoku(board) |
| 100 | + |
| 101 | + if solution: |
| 102 | + print("The solved Sudoku puzzle:") |
| 103 | + print_board(solution) |
| 104 | + else: |
| 105 | + print("No solution found.") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + main() |
0 commit comments