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!