|
| 1 | +def is_valid(board, row, col, num): |
| 2 | + # Check if num is in the same row or column |
| 3 | + for i in range(3): |
| 4 | + if board[row][i] == num or board[i][col] == num: |
| 5 | + return False |
| 6 | + return True |
| 7 | + |
| 8 | + |
| 9 | +def find_empty(board): |
| 10 | + # Return the next empty cell (row, col) or None if full |
| 11 | + for row in range(3): |
| 12 | + for col in range(3): |
| 13 | + if board[row][col] == 0: |
| 14 | + return row, col |
| 15 | + return None |
| 16 | + |
| 17 | + |
| 18 | +def solve_sudoku(board, depth=0): |
| 19 | + indent = " " * depth # Visual indent for stack level |
| 20 | + empty = find_empty(board) |
| 21 | + |
| 22 | + if not empty: |
| 23 | + print(f"{indent}✔️ Solved at depth {depth}") |
| 24 | + return True |
| 25 | + |
| 26 | + row, col = empty |
| 27 | + for num in range(1, 4): |
| 28 | + if is_valid(board, row, col, num): |
| 29 | + print(f"{indent}Trying {num} at ({row}, {col}) [depth {depth}]") |
| 30 | + board[row][col] = num |
| 31 | + print(f"{indent}Board state: {board}") |
| 32 | + |
| 33 | + if solve_sudoku(board, depth + 1): |
| 34 | + return True |
| 35 | + |
| 36 | + print(f"{indent}Backtracking {num} at ({row}, {col}) [depth {depth}]") |
| 37 | + board[row][col] = 0 |
| 38 | + |
| 39 | + return False |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + # Example Sudoku board (3x3) with some numbers filled in |
| 44 | + # 0 represents an empty cell |
| 45 | + # This is a simplified version of a Sudoku board for demonstration purposes |
| 46 | + # In a real Sudoku, the board would be 9x9 and have more complex rules |
| 47 | + |
| 48 | + # simplified 3x3 Sudoku board for demonstration, which is not going to call Backtracking |
| 49 | + board = [ |
| 50 | + [0, 2, 0], |
| 51 | + [0, 0, 1], |
| 52 | + [3, 0, 0] |
| 53 | + ] |
| 54 | + |
| 55 | + ''' |
| 56 | + Starting to solve the Sudoku... |
| 57 | + Trying 1 at (0, 0) [depth 0] |
| 58 | + Board state: [[1, 2, 0], [0, 0, 1], [3, 0, 0]] |
| 59 | + Trying 3 at (0, 2) [depth 1] |
| 60 | + Board state: [[1, 2, 3], [0, 0, 1], [3, 0, 0]] |
| 61 | + Trying 2 at (1, 0) [depth 2] |
| 62 | + Board state: [[1, 2, 3], [2, 0, 1], [3, 0, 0]] |
| 63 | + Trying 3 at (1, 1) [depth 3] |
| 64 | + Board state: [[1, 2, 3], [2, 3, 1], [3, 0, 0]] |
| 65 | + Trying 1 at (2, 1) [depth 4] |
| 66 | + Board state: [[1, 2, 3], [2, 3, 1], [3, 1, 0]] |
| 67 | + Trying 2 at (2, 2) [depth 5] |
| 68 | + Board state: [[1, 2, 3], [2, 3, 1], [3, 1, 2]] |
| 69 | + ✔️ Solved at depth 6 |
| 70 | + [1, 2, 3] |
| 71 | + [2, 3, 1] |
| 72 | + [3, 1, 2] |
| 73 | + ''' |
| 74 | + |
| 75 | + # simplified 3x3 Sudoku board for demonstration, which is going to call Backtracking |
| 76 | + tboard = [ |
| 77 | + [0, 0, 0], |
| 78 | + [1, 0, 3], |
| 79 | + [0, 0, 0] |
| 80 | + ] |
| 81 | + |
| 82 | + ''' |
| 83 | + Starting to solve the Sudoku... |
| 84 | + Trying 2 at (0, 0) [depth 0] |
| 85 | + Board state: [[2, 0, 0], [1, 0, 3], [0, 0, 0]] |
| 86 | + Trying 1 at (0, 1) [depth 1] |
| 87 | + Board state: [[2, 1, 0], [1, 0, 3], [0, 0, 0]] |
| 88 | + Backtracking 1 at (0, 1) [depth 1] |
| 89 | + Trying 3 at (0, 1) [depth 1] |
| 90 | + Board state: [[2, 3, 0], [1, 0, 3], [0, 0, 0]] |
| 91 | + Trying 1 at (0, 2) [depth 2] |
| 92 | + Board state: [[2, 3, 1], [1, 0, 3], [0, 0, 0]] |
| 93 | + Trying 2 at (1, 1) [depth 3] |
| 94 | + Board state: [[2, 3, 1], [1, 2, 3], [0, 0, 0]] |
| 95 | + Trying 3 at (2, 0) [depth 4] |
| 96 | + Board state: [[2, 3, 1], [1, 2, 3], [3, 0, 0]] |
| 97 | + Trying 1 at (2, 1) [depth 5] |
| 98 | + Board state: [[2, 3, 1], [1, 2, 3], [3, 1, 0]] |
| 99 | + Trying 2 at (2, 2) [depth 6] |
| 100 | + Board state: [[2, 3, 1], [1, 2, 3], [3, 1, 2]] |
| 101 | + ✔️ Solved at depth 7 |
| 102 | + [2, 3, 1] |
| 103 | + [1, 2, 3] |
| 104 | + [3, 1, 2] |
| 105 | + ''' |
| 106 | + |
| 107 | + print("Starting to solve the Sudoku...") |
| 108 | + |
| 109 | + if solve_sudoku(board): |
| 110 | + for row in board: |
| 111 | + print(row) |
| 112 | + else: |
| 113 | + print("No solution exists.") |
0 commit comments