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.
For this problem, we need to be tempted to do, for "." Place, we try different numbers to see if this is a valid sudoku, for the 11th line, we first see whether it is a valid Sudoku, and then recursive, if all the subsequent temptations are right, then you can fill in this number, otherwise you should return the location to "." And try the next number if there is no solution, then return false
1 Public voidSolvesudoku (Char[] board) {2 This. Sudokusingle (board);3 }4 Private BooleanSudokusingle (Char[] board) {5 for(inti=0;i<9;i++){6 for(intj=0;j<9;j++){7 CharCurr=Board[i][j];8 if(curr== '. ')){9 for(intk=1;k<=9;k++){TenBoard[i][j]=string.valueof (k). charAt (0); One if( This. Isvalidsudoku (Board, I, J) && This. Sudokusingle (board)) A { - return true; - } theBoard[i][j]= '. '; - } - return false; - } + } - } + return true; A } at Private BooleanIsvalidsudoku (Char[] board,intRowintColum) { - for(intj=0;j<9;j++) - if(J! = Colum && Board[row][j] = =Board[row][colum]) - return false; - - for(inti=0;i<9;i++) in if(I! = row && Board[i][colum] = =Board[row][colum]) - return false; to + intGridrow = row/3*3, Gridcol = colum/3*3; - for(inti=0;i<3;i++) the for(intj=0;j<3;j++) * if(Gridrow + I! = row && Gridcol + J! = Colum && Board[gridrow + i][gridcol + j] = =Board[row][colum]) $ return false; Panax Notoginseng return true; -}
Leetcode Sudoku Solver