Skip to main content

Sudoku Solver

Problem

Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

The '.' character indicates empty cells.

Solution Approach

Use Backtracking and find all the possible states of sudoku board and pick the valid one accordingly.

Expected Time complexity: O(99)O(9^9)

Click - to see solution code
class Solution {
public:
bool check(vector<vector<char>>& board, int i, int j, char val) {
int row = i - i % 3, col = j - j % 3;
for (int k = 0; k < 9; k++)
if (board[k][j] == val) return false;
for (int k = 0; k < 9; k++)
if (board[i][k] == val) return false;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (board[x + row][y + col] == val) return false;
}
}
return true;
}

bool solveSudoku(vector<vector<char>>& board, int i = 0, int j = 0) {
if (i == 9) return true;
if (j == 9) return solveSudoku(board, i + 1, 0);
if (board[i][j] != '.') return solveSudoku(board, i, j + 1);

for (char c = '1'; c <= '9'; c++) {
if (check(board, i, j, c)) {
board[i][j] = c;
if (solveSudoku(board, i, j + 1)) return true;
board[i][j] = '.';
}
}
return false;
}
};