代码
public class Sudoku {
public static void main(String[] args) {
int[][] done = create();
int[][] problem = getProblemSudoku(done);
System.out.println("============================= 未完成的数独 =============================");
for (int i = 0; i < 9; i++) {
System.out.println(Arrays.toString(problem[i])
.replace("[", "")
.replace("]", "")
.replace(",", " ")
.replace("0", " "));
}
System.out.println("============================= 答案在下面 =============================");
System.out.println("=================================||================================");
System.out.println("=================================||=================================");
System.out.println("=================================\\/=================================");
for (int i = 0; i < 9; i++) {
System.out.println(Arrays.toString(done[i]).replace("[", "").replace("]", "").replace(",", " "));
}
}
private static int[][] getProblemSudoku(int[][] sudoku) {
int[][] problemSudoku = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
problemSudoku[i][j] = sudoku[i][j];
}
}
for (int i = 0; i < 60; i++) {
int index1 = (int) (Math.random() * (problemSudoku.length));
int index2 = (int) (Math.random() * (problemSudoku.length));
problemSudoku[index1][index2] = 0;
}
return problemSudoku;
}
public static int[][] create() {
int[][] sudoku = new int[9][9];
sudoku[0] = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
reSort(sudoku[0]);
for (int i = 1; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudoku[i][j] = sudoku[i - 1][(j + 1) / 9 == 1 ? 0 : j + 1];
}
}
int[][] results = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
results[j / 3 + 3 * (i % 3)][j % 3 + 3 * (i / 3)] = sudoku[i][j];
}
}
rowResort(results);
colResort(results);
return results;
}
public static void reSort(int[] arr) {
for (int i = 0; i < 20; i++) {
int index1 = (int) (Math.random() * (arr.length - 1) + 1);
int index2 = (int) (Math.random() * (arr.length - 1) + 1);
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
}
public static void rowResort(int[][] arr) {
for (int i = 0; i < 10; i++) {
int index1 = (int) (Math.random() * (arr.length - 1) + 1);
int index2 = (int) (Math.random() * 2 + 1) + 3 * (index1 / 3);
int[] temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
}
public static void colResort(int[][] arr) {
for (int i = 0; i < 10; i++) {
int index1 = (int) (Math.random() * (arr.length - 1) + 1);
int index2 = (int) (Math.random() * 2 + 1) + 3 * (index1 / 3);
for (int j = 0; j < 9; j++) {
int temp = arr[j][index1];
arr[j][index1] = arr[j][index2];
arr[j][index2] = temp;
}
}
}
}
执行结果
============================= 未完成的数独 =============================
1 9 7 8 4
5 9 7
6 2
2 1 8 9 7 3 5 4
7 4 2
3 1 9 8
7 3 4 1
4 6 2 5
1 5 8
============================= 答案在下面 =============================
=================================||================================
=================================||=================================
=================================\/=================================
1 9 7 3 8 5 6 4 2
8 5 3 2 4 6 9 1 7
4 6 2 7 1 9 5 8 3
6 2 1 8 9 7 3 5 4
9 7 8 4 5 3 2 6 1
5 3 4 1 6 2 7 9 8
7 8 5 6 3 4 1 2 9
3 4 6 9 2 1 8 7 5
2 1 9 5 7 8 4 3 6
Process finished with exit code 0