[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.

Solution:

http://blog.csdn.net/zxzxy1988/article/details/8586289

1  Public classSolution {2      Public voidSolvesudoku (Char[] board) {3 mysudokusolver (board);4     }5 6     Private BooleanMysudokusolver (Char[] board) {7         //TODO auto-generated Method Stub8          for(inti=0;i<board.length;++i) {9              for(intj=0;j<board[0].length;++j) {Ten                 if(board[i][j]== '. ')){ One                      for(intk=1;k<=9;++k) { ABoard[i][j]= (Char) (k + ' 0 '); -                         if(IsValid (BOARD,I,J) &&mysudokusolver (board)) { -                             return true; the                         } -Board[i][j]= '. '; Don't forget the details here!! , you put the "." Behind. Changed to a number, and found that do not go down, have to backtrack, then you
We have to change this number back to "." Ah
- } - return false; + } - } + } A return true; at } - - Private BooleanIsValid (Char[] board,intRowintCol) { - //TODO auto-generated Method Stub - for(inti=0;i<board.length;++i) { - if(i!=row&&board[i][col]==Board[row][col]) in return false; - } to for(inti=0;i<board[0].length;++i) { + if(i!=col&&board[row][i]==Board[row][col]) - return false; the } * for(intI= (ROW/3) *3;i< (row/3+1) *3;++i) { $ for(intj= (COL/3) *3;j< (col/3+1) *3;++j) {Panax Notoginseng if(i!=row&&j!=col&&board[i][j]==Board[row][col]) - return false; the } + } A return true; the } +}

[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.