Solving all the solutions of Sudoku, Java programming implementation

Source: Internet
Author: User

Sudoku is a test of eyesight and logic of the small game, the key in the "single" word, can not be repeated, the block can not be repeated. Today I will introduce a "loop + recursion + backtracking" method to use Java program for us to complete Sudoku.

The code is then explained:

1 ImportJava.util.HashMap; 2 ImportJava.util.Map; 3 4 public classT2 {5 public static final int n=3; 6 public static voidMain (string[] args) {7 int x[][]={8 {0,2,0,0,0,9,0,1,0,0}, 9 {5,0,6,0,0,0,3,0,9,0},10 {0,8,0,5,0,2,0,6,0,0},11 {0,0,5,0,7,0,1,0,0,0},12 {0,0,0,2,0,8,0,0,0,0},13 {0,0,4,0,1,0,8,0,0,0},14 {0,5,0,8,0,7,0,3,0,0},15 {7,0,2,3,0,0,4,0,5,0},16 {0,4,0,0,0,0,0,7,0,0},17};18 function (x,0,0); 20 21}22-private static void function (int[][] x, int r, intc) {if (r>=X.length) {25Show (x);; 27}28 if (c==0&& (r==x.length/n| | r==x.length/n*2| | r==X.length)) {if (!Checkedbox (X,r)) {return; 31};32 33}34 if (c>=X.length) {function (x, r+1, 0);; 37}38 if (x[r][c]==0{<= (int i = 1; I x.length; i++) {if(Checked (x,r,c,i)) {x[r][c]=i;43 function (x, R, c+1); x[r][c]=0; 45}46}47}else{c+1 function (x, R,); 49}50}51 private Static Boolean Checkedbox (int[][] x, intR) {for (int k = 0; k < x.length; k+=x.length/N) {map<integer, integer> map=new hashmap<>(); a. (int i = r-n; I < R; i++) {for (int j = k; J < k+x.length/n; J + +) {Map.containskey (X[i][j]) {x[i][j return false ; }59 map.put (+), 1 ); }62  x }64 return true ; + }66-Private static Boolean checked (int[][] ×, int r, int c, int  i) {(int j = 0; J < X.length; J + + ) {x[j][c]==  i) {}72 return false ; F (x[r][j]==  i) {return false ; Voi }75 }76 return True ; }78 D Show (int  [] x) {x.length (int i = 0; i <; i++ ) {Bayi for (int j = 0; J < X.length; J + + ) {8 2 System.out.print (x[i][j]+ "" );}84   System.out.println ();. }86  System.out.println ( ); }88               

There are five functions in the class, one is the main function is not many, the other is the recursive principal function functions, is the key to solve the Sudoku, the main logic that embodies the loop + recursion + backtracking; The three and four are all an identification function, some more complex judgment logic pulls it out to write the identification function can increase the readability of the Code ; Five is a simple printing function.

The actual meaning of the function is to fill in the number of x[r][c], or more precisely to fill in the X array of (R,C) coordinates after all the numbers, the internal basic structure is 4 side by side if, the first if is said if I fill the line number exceeds the maximum value to print the entire array The second if is to say that when I fill in lines 3rd, 6, 9 the first number of rows to check the top of the three rows of the box is not a duplicate number; the third if is that I fill in the last element, to go to the beginning of the next line, the fourth if is that the current coordinate is 0 when the loop is started, Otherwise skip over and fill in the next number.

The Checkedbox function takes advantage of a very ingenious duplicate checking thought----loop nested map set of first-look-and-load thinking. Of course, you never heard of this thought, because it was my name. Note that I set the value for map in line 59th by loading the value of the array in the map key, so that the ContainsKey function of the map collection is used for line 56th. Of course, I do this to check the weight of the block is a little show operation, in fact, with a length of 9 array can also.

The checked function and the show function are very simple and are not mentioned much.

I give the Sudoku example is a single result of the Sudoku, is more difficult, if you are interested, you can search the other Sudoku to test this code it!!

This method can enumerate all the solutions of a Sudoku.

Expand:

This loop + recursive + backtracking mode can solve all the problems of filling in the numbers according to the rules, for example: nine Gongge fill in 1-9 to add equal to the sum, 16 of the temple filled 1-16 and so on.

  

Solving all the solutions of Sudoku, Java programming implementation

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.