Title Address: https://leetcode.com/problems/sudoku-solver/
Problem Analysis: Use the most stupid method, the line priority traversal need to fill the empty, use 1 to 9 to try, and then determine whether to meet the conditions, if not meet the criteria to try the next number, if the condition is eligible to try to fill the next empty. Implemented using recursive methods.
Topic Answer:
Public classSolution { Public voidSolvesudoku (Char[] board) {Solvesudoku (board,0,0); } Public BooleanSolvesudoku (Char[] board,intRowintcolumn) { if(Row >= 9){ return true; } if(Column >= 9){ returnSolvesudoku (board,row+1,0); } if(Board[row][column] = = '. '){ for(intK = 1;k<=9;k++) {Board[row][column]= (Char) (' 0 ' +k); if(IsValid (Board,row,column) &&solvesudoku (board,row,column+1)){ return true; } Board[row][column]= '. '; } return false; }Else { returnSolvesudoku (board,row,column+1); } } Private BooleanIsValid (Char[] board,intRowintcolumn) { for(inti = 0;i<9;i++){ if(I!=row && board[i][column] = =Board[row][column]) { return false; } } for(inti = 0;i<9;i++){ if(I!=column && board[row][i] = =Board[row][column]) { return false; } } intBoxstartrow = (ROW/3); intBoxstartcolumn = (COLUMN/3); for(inti = boxstartrow;i<boxstartrow+3;i++){ for(intj = boxstartcolumn;j<boxstartcolumn+3;j++){ if(i!=row&&j!=column&&board[i][j]==Board[row][column]) { return false; } } } return true; }}
Leetcode Sudoku Solver