Sudoku Solver gives no output

Here is my code:

that is because your solver is not solving the sudoku, i.e., never reaching the case of i == n.

yes I have figured that out but why is that?

Your canBePlaced() function is wrong.
Note that, if you set board[i][j] = num;, then

    for (int k = 0; k < n; k++)
    {
        if (board[i][k] == num or board[k][j] == num)
        {
            return false;
        }
    }

will always return false.

can you please make the changes to make it correct?

I am sorry, my bad.
Your code is correct.

But please note that not all sudokus are solvable and your example isn’t.

thank you so much bro, my board was wrong. It should be:

1 Like

int board[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} }; I spent hours to debug my code :frowning:

1 Like

Yes, me too :frowning:

1 Like