Leetcode Sudoku Solver

Source: Internet
Author: User

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.

For this problem, we need to be tempted to do, for "." Place, we try different numbers to see if this is a valid sudoku, for the 11th line, we first see whether it is a valid Sudoku, and then recursive, if all the subsequent temptations are right, then you can fill in this number, otherwise you should return the location to "." And try the next number if there is no solution, then return false

1   Public voidSolvesudoku (Char[] board) {2              This. Sudokusingle (board);3         }4      Private BooleanSudokusingle (Char[] board) {5           for(inti=0;i<9;i++){6               for(intj=0;j<9;j++){7                  CharCurr=Board[i][j];8                  if(curr== '. ')){9                       for(intk=1;k<=9;k++){TenBoard[i][j]=string.valueof (k). charAt (0); One                          if( This. Isvalidsudoku (Board, I, J) && This. Sudokusingle (board)) A                          { -                              return true; -                          } theBoard[i][j]= '. '; -                      } -                      return false; -                  } +              } -          } +          return true; A      } at      Private BooleanIsvalidsudoku (Char[] board,intRowintColum) { -           for(intj=0;j<9;j++)   -                 if(J! = Colum && Board[row][j] = =Board[row][colum]) -                     return false;  -            -              for(inti=0;i<9;i++)   in                 if(I! = row && Board[i][colum] = =Board[row][colum]) -                     return false;  to                +             intGridrow = row/3*3, Gridcol = colum/3*3;  -              for(inti=0;i<3;i++)   the                  for(intj=0;j<3;j++)       *                     if(Gridrow + I! = row && Gridcol + J! = Colum && Board[gridrow + i][gridcol + j] = =Board[row][colum]) $                         return false; Panax Notoginseng             return true;  -}

Leetcode Sudoku Solver

Contact Us

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.