Here is a C++ program to solve a Sudoku puzzle using backtracking:
#include <iostream>
using namespace std;
const int N = 9;
// Function to print the Sudoku grid
void printGrid(char grid[N][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << grid[i][j] << " ";
}
cout << endl;
}
}
// Function to check if the given digit can be placed at the given position
bool isSafe(char grid[N][N], int row, int col, char num) {
// Check if the number is already present in the current row
for (int i = 0; i < N; i++) {
if (grid[row][i] == num) {
return false;
}
}
// Check if the number is already present in the current column
for (int i = 0; i < N; i++) {
if (grid[i][col] == num) {
return false;
}
}
// Check if the number is already present in the current 3x3 sub-grid
int subGridRow = (row / 3) * 3;
int subGridCol = (col / 3) * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (grid[subGridRow + i][subGridCol + j] == num) {
return false;
}
}
}
return true;
}
// Function to solve the Sudoku puzzle using backtracking
bool solveSudoku(char grid[N][N]) {
int row, col;
bool isEmpty = false;
// Find an empty cell in the Sudoku grid
for (row = 0; row < N; row++) {
for (col = 0; col < N; col++) {
if (grid[row][col] == '.') {
isEmpty = true;
break;
}
}
if (isEmpty) {
break;
}
}
// If no empty cell is found, the Sudoku puzzle is solved
if (!isEmpty) {
return true;
}
// Try each digit from 1 to 9 in the current empty cell
for (char num = '1'; num <= '9'; num++) {
if (isSafe(grid, row, col, num)) {
// Place the digit in the current empty cell
grid[row][col] = num;
// Recursively solve the remaining Sudoku puzzle
if (solveSudoku(grid)) {
return true;
}
// If the current digit does not lead to a solution, remove it
grid[row][col] = '.';
}
}
return false;
}
int main() {
char grid[N][N] = {{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
if (solveSudoku(grid)) {
cout << "Sudoku solution:" << endl;
printGrid(grid);
} else {
cout << "No solution exists." << endl;
}
return 0;
}
In this program, we have used a 2D character array to represent the Sudoku grid. The empty cells are denoted by the '.' symbol.
The printGrid() function is used to print the Sudoku grid.
The isSafe() function checks if the given digit can be placed at the given position without violating any of the Sudoku rules.
The solveSudoku() function solves the Sudoku puzzle using backtracking. It finds the first empty cell in the grid, tries each digit from 1 to 9 in that cell, and recursively solves the remaining Sudoku puzzle. If a solution is found, it returns true. Otherwise, it backtracks and tries the next digit.
The main() function initializes the Sudoku grid and calls the solveSudoku() function. If a solution exists, it prints the solved Sudoku grid. Otherwise, it prints a message indicating that no solution exists.