Code implementation of the Sudoku solution applet

Source: Internet
Author: User
This article shares the code of the small program for solving the problem of Sudoku. I used Java to write a small program for solving the problem of 9x9 sudoku. I used the exhaustive method to solve the problem.

The code is as follows:

Package Test; public class SensibleGame {/*** @ param args */int [] [] mainNumber; boolean [] [] flagNumber; public SensibleGame (int [] [] mainNumber) {this. mainNumber = mainNumber; flagNumber = new boolean [9] [9]; for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {if (mainNumber [I] [j] = 0) {flagNumber [I] [j] = false ;} else {flagNumber [I] [j] = true ;}}} public boolean CheckRow (int I, int j) // Check (I, j) Location Whether the row meets the Sudoku condition {// code omitted} public boolean CheckColumn (int I, int j) // Check (I, j) whether the column of the position meets the Sudoku condition {// code omitted} public boolean CheckRound (int I, int j) // Check (I, j) whether the 3x3 grid of the location meets the Sudoku condition {// code omitted} public void run () // The calculation method is left blank {int I = 0; int j = 0; boolean previusflag = false; while (true) {if (I <0 | I> 8 | j <0 | j> 8) {System. out. print ("subscript out of bounds! "); Return;} if (previousFlag = false) {if (flagNumber [I] [j] = true) {previusflag = false; if (I = 8 & j = 8) {System. out. print ("computing end \ n"); break;} else if (I >=0 & I <8 & j = 8) {I = I + 1; j = 0; continue;} else {j = j + 1; continue ;}} boolean flag = false; int k = 0; while (! Flag & k <9) {mainNumber [I] [j] = ++ k; flag = CheckAll (I, j) ;}if (flag = true) {previusflag = false; if (I = 8 & j = 8) {System. out. println ("computing end \ n");} else if (I >=0 & I <8 & j = 8) {I = I + 1; j = 0; continue;} else {j = j + 1; continue ;}} else {previousFlag = true; mainNumber [I] [j] = 0; if (I = 0 & j = 0) {System. out. println ("computing failed \ n");} else if (I> 0 & I <9 & j = 0) {I = I-1; j = 8; continue;} else {J = j-1; continue ;}} else {if (flagNumber [I] [j] = true) {previusflag = true; if (I = 0 & j = 0) {System. out. println ("computing failed \ n"); break;} else if (I> 0 & I <9 & j = 0) {I = I-1; j = 8; continue;} else {j = j-1; continue ;}} boolean flag = false; while (! Flag & mainNumber [I] [j] <9 & mainNumber [I] [j]> 0) {mainNumber [I] [j] = mainNumber [I] [j] + 1; flag = CheckAll (I, j);} if (flag = true) {previusflag = false; if (I = 8 & j = 8) {System. out. println ("computing end \ n"); break;} else if (I >=0 & I <8 & j = 8) {I = I + 1; j = 0; continue;} else {j = j + 1; continue ;}} else {previousFlag = true; mainNumber [I] [j] = 0; if (I = 0 & j = 0) {System. out. println ("computing failed \ n"); break;} else if (I> 0 & I <9 & j = 0) {I = I-1; j = 8; continue;} else {j = j-1; continue ;}}} public void Print () // Print {int k = 0; for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {System. out. print (mainNumber [I] [j]); if (++ k) % 9 = 0) {System. out. println ("") ;}}} public static void main (String [] args) {// TODO Auto-generated method stub int [] [] mainNumber = {0, 9, 0, 0, 0}, {, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 6, 0, 0, 0, 0, 5}, {0, 4, 0, 0, 0}, {0, 0, 0, 0, 0 },{, 0 },{, 6 },}; SensibleGame sensibleGame = new SensibleGame (mainNumber); sensibleGame. run (); sensibleGame. print ();}}

The running result is as follows:

Computing ends
493516728
762498351
185372469
837925614
216843975
549761283
654137892
921684537
378259146

The above is the detailed content of the code implemented by the Sudoku solution applet. For more information, see other related articles in the first PHP community!

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.