# Solves a Sudoku puzzle < C++>

What will be the output of this code?
C++
#include <iostream>
#include <vector>
using namespace std;

bool isSafe(vector<vector<int>>& grid, int row, int col, int num) {
// Check if the number is already present in the current row or column
for (int i = 0; i < 9; ++i) {
if (grid[row][i] == num || grid[i][col] == num) {
return false;
}
}

// Check if the number is already present in the current 3x3 subgrid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (grid[i + startRow][j + startCol] == num) {
return false;
}
}
}

return true;
}

bool solveSudoku(vector<vector<int>>& grid) {
int row, col;
bool isFilled = true;
for (row = 0; row < 9; ++row) {
for (col = 0; col < 9; ++col) {
if (grid[row][col] == 0) {
isFilled = false;
break;
}
}
if (!isFilled) {
break;
}
}
if (isFilled) {
return true; // Grid is completely filled
}

for (int num = 1; num <= 9; ++num) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return true;
}
grid[row][col] = 0; // Backtrack
}
}
return false; // No solution found
}

void printGrid(vector<vector<int>>& grid) {
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
cout << grid[i][j] << " ";
}
cout << endl;
}
}

int main() {
vector<vector<int>> grid = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};

if (solveSudoku(grid)) {
cout << "Solution:\n";
printGrid(grid);
} else {
cout << "No solution exists.\n";
}

return 0;
}
C++
#include <iostream>
#include <vector>
using namespace std;

bool isSafe(vector<vector<int>>& grid, int row, int col, int num) {
// Check if the number is already present in the current row or column
for (int i = 0; i < 9; ++i) {
if (grid[row][i] == num || grid[i][col] == num) {
return false;
}
}

// Check if the number is already present in the current 3x3 subgrid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (grid[i + startRow][j + startCol] == num) {
return false;
}
}
}

return true;
}

bool solveSudoku(vector<vector<int>>& grid) {
int row, col;
bool isFilled = true;
for (row = 0; row < 9; ++row) {
for (col = 0; col < 9; ++col) {
if (grid[row][col] == 0) {
isFilled = false;
break;
}
}
if (!isFilled) {
break;
}
}
if (isFilled) {
return true; // Grid is completely filled
}

for (int num = 1; num <= 9; ++num) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solveSudoku(grid)) {
return true;
}
grid[row][col] = 0; // Backtrack
}
}
return false; // No solution found
}

void printGrid(vector<vector<int>>& grid) {
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
cout << grid[i][j] << " ";
}
cout << endl;
}
}

int main() {
vector<vector<int>> grid = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};

if (solveSudoku(grid)) {
cout << "Solution:\n";
printGrid(grid);
} else {
cout << "No solution exists.\n";
}

return 0;
}
5 Replies
Marvee Amasiβ’4mo ago
The output will prolly be a 9x9 matrix , let me send a screenshot shot
Solution
Marvee Amasiβ’4mo ago
Camila_99\$\$β’4mo ago
Think youβre right bro , this is the solution π―π§
nour_oudβ’4mo ago
@Marvee Amasi Our smart programmer :clap: :salute:
Marvee Amasiβ’4mo ago
Yeyy π₯ π thanks π
Want results from more Discord servers?