我想用Java编写suduko拼图。我已经有了代码,但需要了解一下。
public static void main(String[] args) {
int[][] arr = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, { 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 }, { 9, 0, 0, 8, 6, 3, 0, 0, 5 }, { 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 }, { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, { 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
int[] rowno = new int[9];
int[] colno = new int[9];
int[] sbnos = new int[9];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
makeAchoice(arr, (i * arr.length) + j + 1, rowno, colno, sbnos, arr[i][j]);
}
}
}
suduko(arr, 1, rowno, colno, sbnos);
}
public static void suduko(int[][] arr, int cellno, int[] rownos, int[] colnos, int[] sbnos) {
if (cellno == 82) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
return;
}
int rno = (cellno - 1) / arr.length;
int cno = (cellno - 1) % arr.length;
if (arr[rno][cno] == 0) {
int rowno = rownos[rno];
int colno = colnos[cno];
int sbno = sbnos[(rno / 3) * 3 + cno / 3];
int no = rowno | colno | sbno;
for (int choice = 1; choice <= 9; choice++) {
if ((no & (1 << choice)) == 0) {
makeAchoice(arr, cellno, rownos, colnos, sbnos, choice);
suduko(arr, cellno + 1, rownos, colnos, sbnos);
unmakeAchoice(arr, cellno, rownos, colnos, sbnos, choice);
}
}
} else {
suduko(arr, cellno + 1, rownos, colnos, sbnos);
}
}
public static void makeAchoice(int[][] arr, int cellno, int[] rownos, int[] colnos, int[] sbnos, int choice) {
int rno = (cellno - 1) / arr.length;
int cno = (cellno - 1) % arr.length;
arr[rno][cno] = choice;
rownos[rno] ^= (1 << choice);
colnos[cno] ^= (1 << choice);
sbnos[(rno / 3) * 3 + cno / 3] ^= (1 << choice);
}
public static void unmakeAchoice(int[][] arr, int cellno, int[] rownos, int[] colnos, int[] sbnos, int choice) {
int rno = (cellno - 1) / arr.length;
int cno = (cellno - 1) % arr.length;
arr[rno][cno] = 0;
rownos[rno] ^= (1 << choice);
colnos[cno] ^= (1 << choice);
sbnos[(rno / 3) * 3 + cno / 3] ^= (1 << choice);
}