Recently, Singapore Prime Minister Lee Hsien Loong also wrote a code published, a general look, unexpectedly, is the code of coefficient alone! Just a few days ago, the main algorithm of Sudoku is to use backtracking. Backtracking method at that time when beginners in the thinking of the comparison twist, not easy to write right. After writing a few backtracking algorithms, I finally got a little bottom in my heart. The backtracking code is typically grown as follows:
voidBacktracking (int[] arr,intBoundary,intCurrentint[] result) { if(current>=boundary)//Arrival Termination Condition { //Determine if result meets the requirements//if compliant, records the result, that is, the value of the resulting array return; } for(inti=0; i<arr.length;i++) {STEP1://Remove the first element from ArrSTEP 2://added to result with element ISTEP 3:backtracking (arr, boundary, current+1, result);//go to next exploreSTEP 4://take the first element out of the resultSTEP 5://Put the first element back in arr } }
Backtracking code features very distinct, first to determine the termination of the condition of recursion, to reach the termination conditions, the inspection results are qualified, qualified records down. And the essence of backtracking is in the following for loop, a total of 5 steps, the last two steps is just the first two steps of the anti-operation, and therefore become ' backtracking '. We can argue that backtracking is an optimization of brute force search (brute).
A little gossip, to see Sudoku Solver question:
/** ***********************question************************** * Write a program to solve a Sudoku puzzle by filling the EM Pty cells. * Empty cells is indicated by the character '. * Assume that there would be is only one unique solution. * *53..7....*6..195...*.98....6.*8... 6 ... 3 * 4. . 8. 3. . 1 * 7 ... 2 ... 6 *. 6 ..... 2 8. * . . . 4 1 9. . 5 * .... 8. . 7 9 * A Sudoku Puzzle ... * 5 3 4 6 7 8 9 1 2 * 6 7 2 1 9 5 3 4 8 * 1 9 8 3 4 2 5 6 7 * 8 5 9 7 6 1 4 2 3 * 4 2 6 8 5 3 7 9 1 * 7 1 3 9 2 4 8 5 6 * 9 6 1 5 3 7 2 8 4 * 2 8 7 4 1 9 6 3 5 * 3 4 5 2 8 6 1 7 9 * * ... and its solution. ***********************************************************/
My code is as follows:
BOOLIsValid (Char*board[9],intRowintColumnCharNumber ) { for(inti =0; I <9; i++){ if(Board[row][i] = =Number ) { return false; } if(Board[i][column] = =Number ) { return false; } } for(inti =0; I <9; i++){ intA = (Row/3) *3+ I/3; intb = (column/3) *3+ i%3; if(Board[a][b] = =Number ) { return false; } } return true;}BOOLSudokusolution (Char*board[9],intRowintcolumn) { if(Row >=9){ return true; } if(Board[row][column]! ='.'){ if(Column >=8){ returnSudukusolution (board, row+1,0); }Else { returnSudukusolution (board, Row, column+1); } } for(intI=0; i<9; i++){ if(IsValid (Board, Row, column, (Char)('1'+i))) {Board[row][column]= (Char)('1'+i); if(Column >=8&& sudukusolution (board, row+1,0)){ return true; }Else if(Sudukusolution (Board, row, column +1)){ return true; } Board[row][column]='.'; } } return false;}voidSolvesudoku (Char*board[9]){ if(Sudokusolution (board,0,0)){ //successfully find the solution}Else { //cannot find a solution } return;}
The code is written in C, where the bool sudokusolution function is clearly a function structure with backtracking.
[Leetcode] Algorithm topic-Sudoku Solver