-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returning is required because we normally store the result in a vector in question of this type, but since it's a bactracking solution, intial and final states of the board will be same, so to avoid we return and stop it from backtracking after solution is made.
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Write a program to solve a Sudoku puzzle by filling the empty cells. | ||
A sudoku solution must satisfy all of the following rules: | ||
Each of the digits 1-9 must occur exactly once in each row. | ||
Each of the digits 1-9 must occur exactly once in each column. | ||
Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid. | ||
The '.' character indicates empty cells. | ||
*/ | ||
|
||
bool isValid(vector<vector<char>>& board, int row, int col, char c) { | ||
// row check | ||
for(int i = 0; i < 9; i++) | ||
if(board[i][col] == c) | ||
return false; | ||
// col check | ||
for(int i = 0; i < 9; i++) | ||
if(board[row][i] == c) | ||
return false; | ||
// box check | ||
int x0 = (row/3) * 3, y0 = (col/3) * 3; | ||
for(int i = 0; i < 3; i++) { | ||
for(int j = 0; j < 3; j++) { | ||
if(board[x0 + i][y0 + j] == c) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool helper(vector<vector<char>> &board, int row, int col) { | ||
// done | ||
if(row == 9) | ||
return true; | ||
// time for next row | ||
if(col == 9) | ||
return helper(board, row + 1, 0); | ||
// already marked | ||
if(board[row][col] != '.') | ||
return helper(board, row, col + 1); | ||
|
||
for(char c = '1'; c <= '9'; c++) { | ||
if(isValid(board, row, col, c)) { | ||
board[row][col] = c; | ||
// without return here, the board reverts to initial state | ||
if(helper(board, row, col + 1)) | ||
return true; | ||
board[row][col] = '.'; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void solveSudoku(vector<vector<char>>& board) { | ||
helper(board, 0, 0); | ||
} |