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.
DFS, always looking, no way to return:
1 classSolution {2 Public:3 voidSolvesudoku (vector<vector<Char>>&Board) {4 Dosudoku (board);5 }6 7 BOOLCheckvalid (vector<vector<Char>>&board,intXinty)8 {9 for(inti =0; I <9; i++){Ten if(i!=x) One if(Board[i][y] = = Board[x][y])return false; A } - - for(intj =0; J <9; J + +){ the if(J! =y) - if(Board[x][j] = = Board[x][y])return false; - } - + for(inti = (x/3) *3; I < (x/3+1) *3; ++i) { - for(intj = (y/3) *3; J < (y/3+1) *3; ++j) { + if((i!=x) | | (J! =y)) A if(Board[x][y] = = Board[i][j])return false; at } - } - return true; - } - - BOOLDosudoku (vector<vector<Char>> &Board) in { - for(introw =0; Row <9; ++row) { to for(intCol =0; Col <9; ++Col) { + if(Board[row][col] = ='.'){ - for(inti =1; I <=9; ++i) { theBoard[row][col] ='0'+i; * if(checkvalid (board, Row, col)) { $ if(Dosudoku (board)) {Panax Notoginseng return true; - } the } +Board[row][col] ='.'; A } the return false; + } - } $ } $ return true; - } -};
Leetcode Oj:sudoku Solver (sudoku game)