Package pers.robert.lanqiaobei05;
Import Java.util.Scanner;
Import Org.omg.CORBA.DATA_CONVERSION;
/** * You must have heard of the Sudoku game.
such as "Figure 1.png", the player needs to be based on the known number on the 9x9 disk, to infer all the remaining space figures, and meet each row, each column, each of the same color nine within the number contains 1-9, do not repeat.
The answer to Sudoku is unique, so many solutions are also called no solutions. The figures in this figure are said to be the difficult subjects that the Finnish mathematician has spent 3 months designing.
But for you who can use computer programming, I'm afraid it's a breeze. The requirements of this topic is the input Sudoku problem, the program output Sudoku unique solution.
We guarantee that all known data are in a valid format and that the problem has a unique solution.
Format requirements, enter 9 lines, 9 characters per line, 0 is unknown, and other numbers are known.
The output is 9 lines, and 9 digits per line represent the solution of Sudoku. For example: input (that is, the title in the picture): 005300000 800000020 070010500 400005300 010070006 003200080 060500009 004000030 000009700 The program should output: 1453276
98 839654127 672918543 496185372 218473956 753296481 367542819 984761235 521839764 again for example, input: 800000000 003600000-070090200 050007000 000045700 000100030 001000068 008500010 090000400 procedures should be output: 812753649 943682175 675491283 154237896 369845721-2 87169534 521974368 438526917 796318452 Resource conventions: Peak memory consumption (including virtual machines) < 256M CPU consumption < 2000ms please output in strict accordance with the requirements, do not superfluous print similar: "Please enter."
. "Superfluous content.
All the code is placed in the same source file, after debugging, the copy is submitted to the source. Note: Do not use package statements.
Do not use jdk1.7 and the above version of the features.
Note: The name of the main class must be: main, otherwise it will be handled in an invalid code. * @aUthor Robert * */public class The06shududemo2 {//Sudoku problem public static void main (string[] args) {Long starttime =
System.currenttimemillis ();
int[][] map = new INT[9][9];
Read the initial state input (map);
Depth-first traversal and timely backtracking of backtrack (map,0,0) are initiated from the first column Long endtime = System.currenttimemillis (); Get End Time System.out.println ("program Run Time:" + (Endtime-starttime) + "MS"); Output program run time}/** * Depth first traversal and backtracking * @param i * @param j */private static void Backtrack (int[][] map,int i, Int j
{//Loop End condition if (i==8&&j==9) {out (map);
Return
} if (j==9) {j=0;
i++;
} if (map[i][j]==0) {for (int k=1;k<=9;k++) {map[i][j]=k;
if (check (map,i,j)) {backtrack (map,i,j+1);
} map[i][j]=0;
}}else{Backtrack (map,i,j+1); } private static Boolean check (int[][] Map,int i,int j) {//Judgment line for (int k=0;k<9;k++) {if (map[i][j]==map[i)
[K]&&j!=k] return false; }//Decision column for (int k=0;k<9;k++) {if (Map[i][j]==MAP[K][J]&&I!=K) return false;
//Determine if there is a duplicate element int dx=i/3*3 in the 3x3 lattice;
int dy=j/3*3; for (int x=dx;x<dx+3;x++) {for (int y=dy;y<dy+3;y++) {if (map[i][j]==map[x][y]&&i!=
X&&j!=y) return false;
} return true; private static void Out (int[][] map) {for (int i=0;i<9;i++) {for (int j=0;j<9;j++) {System.out.print (map[
I][J]);
} System.out.println ();
} private static void input (int[][] map) {Scanner sc = new Scanner (system.in);
String str;
for (int i=0;i<9;i++) {str = Sc.nextline ();
for (int j=0;j<9;j++) {Map[i][j]=str.charat (j)-' 0 '; }
}
}
}
Wonderful: the difference between + + and +1 is so great
i++ returns the value of I and then returns i+1;i=i+1 to the i+1 (I value after the assignment statement):