A Sudoku solver fills a 9 by 9 grid while respecting row, column, and box uniqueness..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | from bisect import bisect_left # Sudoku Backtracking Solver in Python # # PURPOSE # A Sudoku solver fills a 9 by 9 grid while respecting row, column, and box uniqueness. # # METHOD # Validate givens into three occupancy tables, collect empty cells, then try candidate # digits with iterative backtracking. On exhaustion restore each original empty cell to # zero. # # COMPLEXITY # Exponential worst-case search in the number of empty cells; O(81) bounded workspace. # # CONTRACT # board has 81 values in [0,9], where 0 means empty. Returns 1 after solving, 0 if # unsatisfiable, -1 for conflicting givens. On failure leaves board unchanged. # Public APIs infer array lengths except in C, which accepts pointer/length pairs. Vertex # counts and matrix dimensions remain explicit. Arrays must contain signed 32-bit integers. # Sorted order, correct tree topology, and caller-owned data-structure invariants are # preconditions; checking them on every query would change query complexity. # Scalar and shape checks are included. Value-range checks are included where needed to # prevent invalid indexing or nontermination. Allocation sizes are capped at 4,000,000 # workspace entries. Arithmetic outside Python raises an error on 32-bit overflow; Python # uses arbitrary precision. Native allocation failures propagate. In-place outputs may be # partly updated after arithmetic failure. These are single-threaded, in-memory # implementations. # # LANGUAGE DETAILS # Python 3.9+: automatic lengths, native lists, arbitrary-precision arithmetic, and # bisect_left for LIS. Invalid scalar/shape inputs raise ValueError. Integer element types # and sorted/tree invariants remain caller preconditions. # # EXAMPLE # Expected return: 1. # Expected board after call: [5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, # 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, # 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 9]. # Run: python example.py # Tests for this exact source artifact run before publication. The accompanying source # archive includes unit cases, runtime results, and source hashes. def alg_require(condition): if not condition: raise ValueError("invalid algorithm input; see contract") def alg_size(n): alg_require(0 <= n <= 4000000) return n def sudoku_solve(board: list[int]) -> int: alg_require(isinstance(board, list) and len(board) <= 1000000) alg_require(len(board) == 81) for value in board: alg_require(value >= 0 and value <= 9) rows: list[int] = [0] * alg_size(90) cols: list[int] = [0] * alg_size(90) boxes: list[int] = [0] * alg_size(90) empty: list[int] = [0] * alg_size(81) count: int = 0 position: int = 0 cell: int = 0 r: int = 0 c: int = 0 box: int = 0 digit: int = 0 while cell < 81: r = cell // 9 c = cell % 9 box = r // 3 * 3 + c // 3 digit = board[cell] if digit == 0: empty[count] = cell count += 1 else: if rows[r * 10 + digit] != 0 or cols[c * 10 + digit] != 0 or boxes[box * 10 + digit] != 0: return -1 rows[r * 10 + digit] = 1 cols[c * 10 + digit] = 1 boxes[box * 10 + digit] = 1 cell += 1 while position >= 0 and position < count: cell = empty[position] r = cell // 9 c = cell % 9 box = r // 3 * 3 + c // 3 digit = board[cell] if digit != 0: rows[r * 10 + digit] = 0 cols[c * 10 + digit] = 0 boxes[box * 10 + digit] = 0 digit += 1 while digit <= 9 and (rows[r * 10 + digit] != 0 or cols[c * 10 + digit] != 0 or boxes[box * 10 + digit] != 0): digit += 1 if digit == 10: board[cell] = 0 position -= 1 else: board[cell] = digit rows[r * 10 + digit] = 1 cols[c * 10 + digit] = 1 boxes[box * 10 + digit] = 1 position += 1 if position == count: return 1 return 0 board = [5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 0] result = sudoku_solve(board) print(result) # Expected board: [5, 3, 4, 6, 7, 8, 9, 1, 2, 6, 7, 2, 1, 9, 5, 3, 4, 8, 1, 9, 8, 3, 4, 2, 5, 6, 7, 8, 5, 9, 7, 6, 1, 4, 2, 3, 4, 2, 6, 8, 5, 3, 7, 9, 1, 7, 1, 3, 9, 2, 4, 8, 5, 6, 9, 6, 1, 5, 3, 7, 2, 8, 4, 2, 8, 7, 4, 1, 9, 6, 3, 5, 3, 4, 5, 2, 8, 6, 1, 7, 9] print(board) |