|
| 1 | +<h1 align='center'>Sudoku - Solver</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Sudoku Solver](https://leetcode.com/problems/sudoku-solver/) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Explanation |
| 11 | +#### **Problem Statement** |
| 12 | +We need to solve a partially completed Sudoku board represented as a `9x9` grid: |
| 13 | +- Empty cells are represented as `'.'`. |
| 14 | +- Digits `1-9` are filled in some cells. |
| 15 | + |
| 16 | +The solution must satisfy these constraints: |
| 17 | +1. Each digit appears exactly once in each row. |
| 18 | +2. Each digit appears exactly once in each column. |
| 19 | +3. Each digit appears exactly once in each `3x3` subgrid. |
| 20 | + |
| 21 | +#### **Approach** |
| 22 | +This problem is solved using **backtracking**, which involves: |
| 23 | +1. Iteratively checking each cell of the board. |
| 24 | +2. Trying all possible digits (`1` to `9`) in empty cells. |
| 25 | +3. Validating if placing a digit is safe based on the Sudoku rules. |
| 26 | +4. If a valid placement is found, move to the next cell recursively. |
| 27 | +5. If no valid placement is possible, backtrack by removing the digit and trying another. |
| 28 | + |
| 29 | +## Problem Solution |
| 30 | +```cpp |
| 31 | +class Solution { |
| 32 | +public: |
| 33 | + bool isSafe(int row, int col, vector<vector<char>>& board, int value){ |
| 34 | + char ch = value + '0'; |
| 35 | + |
| 36 | + for(int i = 0; i < board[0].size(); i++){ |
| 37 | + if(board[row][i] == ch) return false; |
| 38 | + if(board[i][col] == ch) return false; |
| 39 | + if(board[3*(row/3) + i/3][3*(col/3) + i%3] == ch) return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | + } |
| 43 | + bool solve(vector<vector<char>>& board){ |
| 44 | + int n = board[0].size(); |
| 45 | + |
| 46 | + for(int row = 0; row < n; row++){ |
| 47 | + for(int col = 0; col < n; col++){ |
| 48 | + if(board[row][col] == '.'){ |
| 49 | + for(int val = 1; val <= 9; val++){ |
| 50 | + if(isSafe(row, col, board, val)){ |
| 51 | + char ch = val + '0'; |
| 52 | + board[row][col] = ch; |
| 53 | + if(solve(board)) return true; |
| 54 | + board[row][col] = '.'; |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + void solveSudoku(vector<vector<char>>& board) { |
| 67 | + solve(board); |
| 68 | + } |
| 69 | +}; |
| 70 | +``` |
| 71 | +
|
| 72 | +## Problem Solution Explanation |
| 73 | +
|
| 74 | +#### Function: `isSafe` |
| 75 | +
|
| 76 | +```cpp |
| 77 | +bool isSafe(int row, int col, vector<vector<char>>& board, int value) { |
| 78 | + char ch = value + '0'; |
| 79 | + for (int i = 0; i < board[0].size(); i++) { |
| 80 | + if (board[row][i] == ch) return false; |
| 81 | + if (board[i][col] == ch) return false; |
| 82 | + if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == ch) return false; |
| 83 | + } |
| 84 | + return true; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +- **Purpose**: Validates if placing the given value in the cell `(row, col)` is valid. |
| 89 | +- **Steps**: |
| 90 | + - Converts the value to its character equivalent (e.g., `1 → '1'`). |
| 91 | + - Checks the entire row to see if the digit already exists. |
| 92 | + - Checks the entire column for conflicts. |
| 93 | + - Maps the `(row, col)` to its respective `3x3` subgrid and scans for conflicts. |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +#### Function: `solve` |
| 98 | + |
| 99 | +```cpp |
| 100 | +bool solve(vector<vector<char>>& board) { |
| 101 | + int n = board[0].size(); |
| 102 | + for (int row = 0; row < n; row++) { |
| 103 | + for (int col = 0; col < n; col++) { |
| 104 | + if (board[row][col] == '.') { |
| 105 | + for (int val = 1; val <= 9; val++) { |
| 106 | + if (isSafe(row, col, board, val)) { |
| 107 | + board[row][col] = val + '0'; |
| 108 | + if (solve(board)) return true; |
| 109 | + board[row][col] = '.'; |
| 110 | + } |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return true; |
| 117 | +} |
| 118 | +``` |
| 119 | +
|
| 120 | +- **Purpose**: Recursively solves the Sudoku board using backtracking. |
| 121 | +- **Steps**: |
| 122 | + - Iterates through all cells of the board. |
| 123 | + - Identifies empty cells (`'.'`). |
| 124 | + - Tries placing digits from `1` to `9`. |
| 125 | + - Checks if the placement is valid using `isSafe`. |
| 126 | + - If valid, places the digit and recurses to solve the next cell. |
| 127 | + - If no valid digit works, backtracks by resetting the cell and trying another digit. |
| 128 | + - Returns `true` if the board is solved, or `false` if no solution is possible. |
| 129 | +
|
| 130 | +
|
| 131 | +
|
| 132 | +#### Function: `solveSudoku` |
| 133 | +
|
| 134 | +```cpp |
| 135 | +void solveSudoku(vector<vector<char>>& board) { |
| 136 | + solve(board); |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +- **Purpose**: Initiates the solving process by calling the recursive `solve` function. |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +### **Step 3: Example Walkthrough** |
| 145 | + |
| 146 | +#### Input Board |
| 147 | +``` |
| 148 | +[ |
| 149 | + ['5', '3', '.', '.', '7', '.', '.', '.', '.'], |
| 150 | + ['6', '.', '.', '1', '9', '5', '.', '.', '.'], |
| 151 | + ['.', '9', '8', '.', '.', '.', '.', '6', '.'], |
| 152 | + ['8', '.', '.', '.', '6', '.', '.', '.', '3'], |
| 153 | + ['4', '.', '.', '8', '.', '3', '.', '.', '1'], |
| 154 | + ['7', '.', '.', '.', '2', '.', '.', '.', '6'], |
| 155 | + ['.', '6', '.', '.', '.', '.', '2', '8', '.'], |
| 156 | + ['.', '.', '.', '4', '1', '9', '.', '.', '5'], |
| 157 | + ['.', '.', '.', '.', '8', '.', '.', '7', '9'] |
| 158 | +] |
| 159 | +``` |
| 160 | + |
| 161 | +#### Execution Steps |
| 162 | +1. Start at `(0, 2)`: |
| 163 | + - Try placing `1`. **Fails row check**. |
| 164 | + - Try placing `2`. **Fails subgrid check**. |
| 165 | + - Try placing `4`. **Valid**. Place `4` and proceed to `(0, 3)`. |
| 166 | + |
| 167 | +2. At `(0, 3)`: |
| 168 | + - Try placing `1`. **Valid**. Place `1` and move forward. |
| 169 | + |
| 170 | +3. Continue filling valid digits in empty cells. |
| 171 | + |
| 172 | +4. Backtrack if stuck until a solution is found. |
| 173 | + |
| 174 | +#### Output Board |
| 175 | +``` |
| 176 | +[ |
| 177 | + ['5', '3', '4', '6', '7', '8', '9', '1', '2'], |
| 178 | + ['6', '7', '2', '1', '9', '5', '3', '4', '8'], |
| 179 | + ['1', '9', '8', '3', '4', '2', '5', '6', '7'], |
| 180 | + ['8', '5', '9', '7', '6', '1', '4', '2', '3'], |
| 181 | + ['4', '2', '6', '8', '5', '3', '7', '9', '1'], |
| 182 | + ['7', '1', '3', '9', '2', '4', '8', '5', '6'], |
| 183 | + ['9', '6', '1', '5', '3', '7', '2', '8', '4'], |
| 184 | + ['2', '8', '7', '4', '1', '9', '6', '3', '5'], |
| 185 | + ['3', '4', '5', '2', '8', '6', '1', '7', '9'] |
| 186 | +] |
| 187 | +``` |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +### **Step 4: Time and Space Complexity** |
| 192 | + |
| 193 | +#### **Time Complexity** |
| 194 | +- In the worst case, each of the `81` cells is empty, and we try placing all `9` digits in each cell. |
| 195 | +- Complexity: **O(9^(81))** (theoretical upper bound). |
| 196 | + |
| 197 | +#### **Space Complexity** |
| 198 | +- Recursive stack: **O(81)** in the worst case (depth of recursion for all cells). |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +### **Step 5: Additional Recommendations** |
| 203 | +1. **Optimizations**: |
| 204 | + - Use arrays or bitmasks for faster row, column, and subgrid checks instead of repeatedly scanning the board. |
| 205 | +2. **Debugging**: |
| 206 | + - Print the board state during recursion to visualize progress. |
| 207 | +3. **Practicing**: |
| 208 | + - Try manually solving Sudoku puzzles to understand the constraints better. |
0 commit comments