Sudoku solver why not printing any things

why my code below is not printing anything
#include
#include

using namespace std;

bool canPlace(int in[][9], int i, int j, int number, int n)
{
///for row or column
for(int x = 0; x < n; x++)
{
if((in[i][x] == number) || in[x][j] == number)
{
return false;
}
}

///for subgrid to avoid filling dublicate from 1 to 9

int rt = sqrt(n);
int sx = (i/rt) * rt;
int sy = (j/rt) * rt;

for(int x = sx; i < sx + rt; x++)
{
    for(int y = sy; j < sy + rt; y++)
    {
        if(in[x][y] == number)
        {
            return false;
        }

    }
}

///if possible to place the number into that index
return true;

}

bool solve(int in[9][9], int i, int j, int n)
{
///i == n && j == n
if(i == n)
{
///print the sudoku
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout<<in[i][j]<<" ";
}
cout<<endl;
}
return true;
}

///if j reachess to end of each row then call for next row
if(j == n)
{
    return solve(in, i+1, 0, n);
}

///prefilled value
if(in[i][j] != 0)
{
    return solve(in, i, j + 1, n);
}

///fill the current cell with possible options

for(int number = 1; number <= n; number++)
{
        if(canPlace(in, i, j, number, n))
        {
            in[i][j] = number;
            bool solved =  solve(in, i, j + 1, n);
            if(solved == true)
            {
                return true;
            }
        }
}

in[i][j] = 0;

return false;

}

int main()
{
int in[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, 4},
{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},
};
int n = 9;
solve(in, 0, 0, n);
return 0;
}

provide code link using ide…

i had already posted code as it is.
help me to fixed.

modified code

for(int x = sx; i < sx + rt; x++)
{
for(int y = sy; j < sy + rt; y++)
{
if(in[x][y] == number)
{
return false;
}
}
}


bhai esi galti mt kiyo karo itne bare code likhne k bad … x=i and y=j :slight_smile:

OMG !

okay thanks helping to fix.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like