# [Leetcode] Sudoku Solver

Source: Internet
Author: User

Sudoku Solver

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.

Ideas:

DFS searches for all possible solutions, each with a space starting at 1-9, and the IsValid () function determines whether the space can be placed in this number. If it can, continue with Dfs recursion, if it cannot proceed to the next number. If there is still no feasible solution after traversing 1-9, the preceding space is not filled with the wrong number, only the previous number can be changed back. This traversal, until the last space still satisfies the condition, indicates that a workable solution has been found. A variable flag flag is used to determine if a viable solution is found. If no feasible solution is found, the current number is restored to the initial state (.), and the loop is resumed, and if it is found, it is returned directly and no longer loops, leaving recursively. This flag is important, if the flag is not available, although the possible solution is found, but the rest of the loop will continue to exit recursively, the value of board[i][j] will change, so the results are very strange when the recursion is rolled out.

In fact, there is a method at night, the idea is similar, just to change the DFS () function to return the value, but no longer void. This returns true if a workable solution is found, otherwise false is returned.

Exercises

`classSolution { Public:    BOOLFlag =false;//determine if a workable solution is found    BOOLIsValid (vector<vector<Char> > &board,intRowintCol) {         for(intj=0;j<9; j + +)            if(J!=col && Board[row][col] = =Board[row][j])return false;  for(intI=0;i<9; i++)            if(I!=row && Board[row][col] = =Board[i][col])return false; intx = row/3*3; inty = col/3*3;  for(intI=0;i<3; i++)             for(intj=0;j<3; j + +)                if(X+i!=row && y+j!=col && board[row][col] = = board[x+i][y+J])return false; return true; }    voidDFS (vector<vector<Char> > &Board) {        intK;  for(intI=0;i<9; i++)             for(intj=0;j<9; j + +) {                if(board[i][j]=='.') {                     for(k=0;k<9; k++) {Board[i][j]= k +'1'; if(IsValid (board,i,j)) DFS (board); if(!flag)//If no workable solution is found, the restorationBOARD[I][J] ='.'; Else           //have found a workable solution, return directly, not continue the next cycle                            return; }                    if(k==9)//Maintenance If the fill 1-9 is not satisfied, the previous results have a problem                        return; }                if(i==8&& j==8)//found a workable solution.Flag =true; }    }        voidSolvesudoku (vector<vector<Char> > &Board)    {DFS (board); }};`
View Code

Something:

As in the idea, you can turn the DFS () function into a function that returns a bool type, and feel that this method should be a little more awesome. Just put out two of these methods.

Http://www.cnblogs.com/ganganloveu/p/3828401.html and Http://www.cnblogs.com/panda_lin/archive/2013/11/04/sudoku_solver.html

[Leetcode] Sudoku Solver

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.