Sudoku Solver Algorithm
The Sudoku Solver Algorithm is a computational technique designed to efficiently solve Sudoku puzzles, which are popular logic-based number-placement games. The main objective is to fill a 9x9 grid with numbers from 1 to 9 in such a way that each row, column, and 3x3 subgrid contains all the numbers without repetition. To achieve this, the algorithm employs a combination of strategies, including backtracking, constraint propagation, and other heuristics, to search for a valid solution to the given puzzle.
Backtracking is a crucial aspect of the algorithm, where the solver systematically tries out different combinations of numbers in the empty cells and, upon reaching an invalid configuration or contradiction, it backtracks to the previous decision point and explores alternative paths. The constraint propagation technique helps in reducing the search space by applying constraints (row, column, and subgrid uniqueness) to eliminate possibilities that would lead to an invalid solution. Additionally, the Sudoku Solver Algorithm can incorporate advanced solving techniques such as naked pairs, hidden singles, and X-wing, to make the solving process more efficient. By combining these methods, the algorithm efficiently deduces the correct number assignments to complete the puzzle, providing a valid and unique solution if one exists.
class Solution {
public:
bool visited_cells[20][20];
bool visited_row[20][20];
bool visited_col[20][20];
void solveSudoku(vector<vector<char>>& board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
memset(visited_cells, false, sizeof(visited_cells));
memset(visited_row, false, sizeof(visited_row));
memset(visited_col, false, sizeof(visited_col));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int cell = (i / 3) * 3 + j / 3;
int x = board[i][j] - '0';
visited_cells[cell][x] = true;
visited_row[i][x] = visited_col[j][x] = true;
}
}
}
isValidSudokuHelper(board, 0);
}
bool isValidSudokuHelper(vector<vector<char>>& board, int step) {
if (step == 81) return true;
int row = step / 9;
int col = step % 9;
int cell = (row / 3) * 3 + col / 3;
if (board[row][col] != '.') {
return isValidSudokuHelper(board, step + 1);
}
for (int i = 1; i <= 9; i++) {
if (!visited_cells[cell][i] && !visited_row[row][i] && !visited_col[col][i]) {
visited_cells[cell][i] = true;
visited_row[row][i] = visited_col[col][i] = true;
board[row][col] = '0' + i;
bool flag = isValidSudokuHelper(board, step + 1);
if (flag) return true;
board[row][col] = '.';
visited_cells[cell][i] = false;
visited_row[row][i] = visited_col[col][i] = false;
}
}
return false;
}
};