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.
Solution:
http://blog.csdn.net/zxzxy1988/article/details/8586289
1 Public classSolution {2 Public voidSolvesudoku (Char[] board) {3 mysudokusolver (board);4 }5 6 Private BooleanMysudokusolver (Char[] board) {7 //TODO auto-generated Method Stub8 for(inti=0;i<board.length;++i) {9 for(intj=0;j<board[0].length;++j) {Ten if(board[i][j]== '. ')){ One for(intk=1;k<=9;++k) { ABoard[i][j]= (Char) (k + ' 0 '); - if(IsValid (BOARD,I,J) &&mysudokusolver (board)) { - return true; the } -Board[i][j]= '. '; Don't forget the details here!! , you put the "." Behind. Changed to a number, and found that do not go down, have to backtrack, then you
We have to change this number back to "." Ah
- } - return false; + } - } + } A return true; at } - - Private BooleanIsValid (Char[] board,intRowintCol) { - //TODO auto-generated Method Stub - for(inti=0;i<board.length;++i) { - if(i!=row&&board[i][col]==Board[row][col]) in return false; - } to for(inti=0;i<board[0].length;++i) { + if(i!=col&&board[row][i]==Board[row][col]) - return false; the } * for(intI= (ROW/3) *3;i< (row/3+1) *3;++i) { $ for(intj= (COL/3) *3;j< (col/3+1) *3;++j) {Panax Notoginseng if(i!=row&&j!=col&&board[i][j]==Board[row][col]) - return false; the } + } A return true; the } +}
[Leetcode] Sudoku Solver