what is the error
#include
#include
using namespace std;
bool canPlace(int matrix[][9],int i, int j ,int n, int number){
for(int x=0;x<n;x++){
if(matrix[x][i]==number||matrix[i][x]==number){
return false;
}
}
//subgrid
int rn =sqrt(n);
int sx = (i/rn)*rn;
int sy = (j/rn)*rn;
for(int x=sx;x<sx+rn;x++){
for(int y=sy;y<sy+rn;y++){
if(matrix[x][y]==number){
return false;
}
}
}
return true;
}
bool solveSudoku(int matrix[][10],int i, int j, int n){
//base case
if(i==n){
//print the matrix
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<matrix[i][j];
}
cout<<endl;
}
return true;
}
//case row ends
if(j==n){
return solveSudoku(matrix,i+1,0,n);
}
//skip the pre filled cells
if(matrix[i][j]!=0){
return solveSudoku(matrix,i,j+1,n);
}
//rec case
//fill the current cell with possible options
for(int number=1;number<=n;number++){
if(canPlace(matrix,i,j,n,number)){
//assume
matrix[i][j]=number;
bool couldWeSolve = solveSudoku(matrix,i,j+1,n);
if(couldWeSolve){
return true;
}
}
}
//backtrack
matrix[i][j]=0;
return false;
}
int main(){
int matrix[9][9] = {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};
solveSudoku(matrix,0,0,9);
return 0;}