Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 37SudokuSolver.cpp #174

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Solutions/37SudokuSolver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Solution {
private:
bool helpmeValid(vector<vector<char>>& board, int row, int col, char x)
{
for(int i=0;i<9;i++)
{
if (board[i][col] == x) return false;
if (board[row][i] == x) return false;

//formula:
if (board[3 * (row/3) + i/3][3 * (col/3) + i%3] == x) return false;
}
return true;
}


bool helpme(vector<vector<char>>& board)
{
for (int i=0;i<board.size();i++)
{
for (int j=0;j<board[0].size();j++)
{
if (board[i][j] == '.')
{
for (char c = '1' ; c <= '9'; c++)
{
if (helpmeValid(board,i,j,c))
{
board[i][j] = c;

if (helpme(board) == true) return true;
else board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
public:
void solveSudoku(vector<vector<char>>& board)
{
helpme(board);
}
};