# [Leetcode] [JavaScript] 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.

A Sudoku Puzzle ...

... and its solution numbers marked in red.

https://leetcode.com/problems/sudoku-solver/

Another DFS.

Test case is weaker, only 6, and the deal is solvable.

Each to a lattice first to see whether it is over or not need to fill, otherwise call the Findcandidate method to find all the numbers in the current step, and then start recursion.

Every round of recursion comes back to the default '. ' Write it back, or it will affect the next round of results.

Open a flag record is not the end, if it has been traversed to return, no need to find.

`1 /**2 * @param {character[][]} board3 * @return {void} does not return anything, modify board In-place instead.4  */5 varSolvesudoku =function(board) {6     varIscomplete =false;7DFS (0, 0);8 9     functiondfs (x, y) {Ten         if(iscomplete) { One             return; A         } -         varCandidates =findcandidate (x, y); -         if(x = = = 8 && y = = 8){ the             if(Board[8][8] = = = '. '){ -BOARD[8][8] = candidates[0]; -             } -Iscomplete =true; +             return; -         } +  A         if(Board[x][y]!== '. '){ at             if(Y = = 8){ -DFS (x + 1, 0); -                 return; -}Else{ -DFS (x, y + 1); -                 return; in             } -         } to          for(vari = 0; i < candidates.length; i++){ +Board[x][y] =Candidates[i]; -             if(Y = = 8){ theDFS (x + 1, 0); *}Else{ \$DFS (x, y + 1);Panax Notoginseng             } -             if(!iscomplete) { theBoard[x][y] = '. ';  +}Else{ A                 return; the             } +         } -     } \$  \$     functionfindcandidate (x, y) { -         varSet =NewSet (); -         varcandidate = []; the         varCell =-1; -         //RowWuyi          for(i = 0; i < 9; i++){ theCell =Board[x][i]; -             if(!Set.has (cell)) { Wu Set.add (cell); -             }    About         } \$         //column -          for(i = 0; i < 9; i++){ -Cell =Board[i][y]; -             if(!Set.has (cell)) { A Set.add (cell); +             }   the         } -         //Square \$         varOffsetX = parseint (X/3) * 3; the         varOffsetY = parseint (Y/3) * 3; the          for(i = 0; I <= 2; i++){ the              for(j = 0; J <= 2; j + +){ theCell = board[i + offsetx][j +OffsetY]; -                 if(!Set.has (cell)) { in Set.add (cell); the                 } the             } About         } the         //Find candidate the          for(i = 1; I <= 9; i++){ the             if(!set.has (i + ""))){ +Candidate.push (i + "")); -             } the         }Bayi         returncandidate; the     }  the};`

With a UT

`1 functionTest () {2     varMap = [3[' 5 ', ' 3 ', '. ', '. ', ' 7 ', '. ', '. ', '. ', '. '],4[' 6 ', '. ', '. ', ' 1 ', ' 9 ', ' 5 ', '. ', '. ', '. '],5['. ', ' 9 ', ' 8 ', '. ', '. ', '. ', '. ', ' 6 ', '. '],6[' 8 ', '. ', '. ', '. ', ' 6 ', '. ', '. ', '. ', ' 3 '],7[' 4 ', '. ', '. ', ' 8 ', '. ', ' 3 ', '. ', '. ', ' 1 '],8[' 7 ', '. ', '. ', '. ', ' 2 ', '. ', '. ', '. ', ' 6 '],9['. ', ' 6 ', '. ', '. ', '. ', '. ', ' 2 ', ' 8 ', '. '],Ten['. ', '. ', '. ', ' 4 ', ' 1 ', ' 9 ', '. ', '. ', ' 5 '], One['. ', '. ', '. ', '. ', ' 8 ', '. ', '. ', ' 7 ', ' 9 '] A     ] -  - Solvesudoku (map); the  -      for(varm = 0; M < map.length; m++){ -         varline = ""; -          for(varn = 0; n < map[m].length; n++){ +Line + = (Map[m][n] + ","); -         } + Console.log (line); A     } at}`

[Leetcode] [JavaScript] Sudoku Solver

Related Keywords:
Related Article

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.