Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells is indicated by the character ‘.‘
.
Assume that there would be is only one unique solution.
A Sudoku Puzzle ...
... and its solution numbers marked in red.
classSolution { Public: voidSolvesudoku (vector<vector<Char>> &Board) {Backtracking (board,0); } BOOLBacktracking (vector<vector<Char>> &board,intLine ) { //Find first empty cell for(inti = line; i<9; i++) { for(intj =0; j<9; J + +) { if(board[i][j]!='.')Continue; //First empty cell is found for(intK =1; k<=9; k++)//backtracking method to find the number that should be filled in the empty unit { if(!isvalid (board,i,j,k+'0'))Continue; BOARD[I][J]= k +'0'; if(Backtracking (board,i))return true; } Board[i][j]='.'; return false;//If no valid value is found } } return true;//If no empty cell is found } //Write a separate check function for backtracking BOOLIsValid (vector<vector<Char>> &board,intLineintColumnCharvalue) { //check a grid of nine Gongge intUpperboard = line/3*3; intLeftboard = column/3*3; for(inti =0; i<3; i++) { for(intj =0; j<3; J + +) { if(Board[upperboard+i][leftboard+j] = = value)return false; } } //Check Column for(inti =0; i<9; i++) { if(Board[line][i] = = value)return false; } for(inti =0; i<9; i++) { if(Board[i][column] = = value)return false; } return true; }};
Panax Sudoku Solver (Graph; WFS)