Skip to content

37. Sudoku Solver.cpp#59

Closed
shafaq16 wants to merge 1 commit intoSjxSubham:mainfrom
shafaq16:main
Closed

37. Sudoku Solver.cpp#59
shafaq16 wants to merge 1 commit intoSjxSubham:mainfrom
shafaq16:main

Conversation

@shafaq16
Copy link
Contributor

@shafaq16 shafaq16 commented Oct 2, 2025

Approach: Backtracking

I use a backtracking algorithm to fill the empty cells ('.') one by one with digits '1' to '9'.
For each empty cell, try all possible digits.
Before placing, check if it's valid (row, column, and 3×3 subgrid rules).
If valid → place it, then recursively try to solve the rest.
If no digit works → backtrack (undo placement and try another option).
If all cells are filled correctly → solution is found.
This ensures we systematically explore possibilities until we reach the correct valid Sudoku configuration.

Intuition

Think of Sudoku solving as filling a puzzle step by step with trial and error:
Whenever you see an empty cell, you test numbers just like a human would.
If the number violates Sudoku rules, skip it.
If a number works, assume it’s correct and move forward.
If later you hit a dead-end, backtrack to the previous step and try another option.

This mimics how humans solve Sudoku, but the computer does it exhaustively and efficiently.

Solution in Code (C++)

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        backtrack(board);
    }

private:
    bool backtrack(vector<vector<char>>& board) {
        for (int r = 0; r < 9; r++) {
            for (int c = 0; c < 9; c++) {
                if (board[r][c] == '.') {
                    for (char ch = '1'; ch <= '9'; ch++) {
                        if (isValid(board, r, c, ch)) {
                            board[r][c] = ch;
                            if (backtrack(board)) return true; // continue with this placement
                            board[r][c] = '.'; // undo if failed
                        }
                    }
                    return false; // no valid digit, backtrack
                }
            }
        }
        return true; // all cells filled
    }

    bool isValid(vector<vector<char>>& board, int r, int c, char ch) {
        // Row & Column check
        for (int i = 0; i < 9; i++) {
            if (board[r][i] == ch) return false;
            if (board[i][c] == ch) return false;
        }
        // 3x3 sub-box check
        int startRow = (r / 3) * 3;
        int startCol = (c / 3) * 3;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[startRow + i][startCol + j] == ch) return false;
            }
        }
        return true;
    }
};

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo

@shafaq16 shafaq16 mentioned this pull request Oct 2, 2025
4 tasks
@SjxSubham
Copy link
Owner

Well @shafaq16

Star the repo ⭐...

Happy HAcktober... keep contributing

@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 2, 2025
@SjxSubham
Copy link
Owner

@shafaq16 can you solve #51 problem ...this one already solved by soemone

@SjxSubham SjxSubham closed this Oct 2, 2025
@shafaq16
Copy link
Contributor Author

shafaq16 commented Oct 2, 2025

hey @SjxSubham pls assign this to me as you told me before to solve this one

@shafaq16
Copy link
Contributor Author

shafaq16 commented Oct 2, 2025

Hey @SjxSubham kindly check my pr #63 under #51 problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants