Leetcode:sudoku Solver

Source: Internet
Author: User

Sudoku Solver, test instructions as follows:

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.

That is to solve the ordinary Sudoku, began to think of a very naive simulation method, from only one possibility is the number can be determined to start filling, the result of course is not out of the way, because some places are more than one solution, because this problem in the classification of backtracking, and then think again.

Since there are a variety of solutions, it is possible to try the past, a solution can be output.

The process is as specific as can be understood:

Traverse the entire Sudoku table, once encountered a vacancy, fill in a 1, and then determine whether the 1 is in the row and column and the small nine Gongri is feasible, if feasible, go down, if not feasible, fill 2, repeat the above operation, have tried until 9.

First put the code:

public static void Solvesudoku (char[][] board) {Sudoku (board); } Static Boolean Sudoku (char[][] board) {for (int. i=0;i<9;i++) {for (int j=0;j<9;j++) {if (board[i][j] = = '. ') {for (int k=1;k<=9;k++) {Board[i][j] = (char) (' 0 ' +k), if (IsValid (board, I, J) && Sudoku (board)) {return true;} BOARD[I][J] = '. ';} return false;}}} return true;} Static Boolean IsValid (char[][] Board,int I,int j) {for (int a=0;a<9;a++) {if (board[i][j] = = Board[i][a] && a!=j Return false;if (board[i][j] = = Board[a][j] && a!=i) return false;} for (int a=3* (I/3), a<3* (i/3+1), a++) {for (int b=3* (J/3); b<3* (j/3+1); b++) {if (a!=i && b!=j && Board A [b] = = Board[i][j]) {return false;}}} return true;} public static void Main (string[] args) {char[][] a = {{'. ', '. ', ' 9 ', ' 7 ', ' 4 ', ' 8 ', '. ', '. ', '. ', ' {' 7 ', '. ', '. ', '. ', '. ', '. ' , '. ', '. ', '. '}, {'. ', ' 2 ', '. ', ' 1 ', '. ', ' 9 ', '. ', '. ', '. '}, {'. ', '. ', ' 7 ', '. ', '. ', '. ', ' 2 ', ' 4 ', '. '}, {'. ', ' 6 ', ' 4 ', ' . ', ' 1 ', '. ', ' 5 ', ' 9 ', '. '}, {'. ', ' 9 ', ' 8 ', '. ', '. ', '. ', ' 3 ', '. ', '. ', {'. ', '. ', '. ', ' 8 ', '. ', ' 3 ', '. ', ' 2 ', '. '}, {'. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ' , ' 2 ', ' 7 ', ' 5 ', ' 9 ', '. ', '. '}; Solvesudoku (a); for (int. i=0;i<9;i++) {for (int j=0;j<9;j++) {System.out.print (a[i][j]+ "");} System.out.println ();}}
The secret of the code is only a tacit.

Leetcode:sudoku Solver

Related Article

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.