Use exhaustive method to solve Sudoku problem. The general idea is:
1. First pick out the existing numbers and do not deal with them.
2. The enumeration of unknown numbers starts from top to bottom and from left to right. If the first number is [0] [0], first assume that it is 1, judge whether there are duplicates in its row, then judge whether there are duplicates in its column, and finally judge whether there are duplicates in its 9 house. If there is no repetition, the next unknown. If there is repetition, it will increase.
3. If the number is increased to 10, it means that the previous number is wrong, and it goes back to the previous unknown number.
4. If the first number in a row increases to 10, it goes back to the last number in the previous row.
ps. this kind of approach feels so retarded that some tall algorithms can't understand...
package com.zttech.demo.sudoku;
import static org.hamcrest.CoreMatchers.instanceOf;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import com.zttech.demo.excel.ExcelOperate;
/**
* Data solution
* @author guoqicheng
* @date 2018 April 20th 2013
*/
public class Sudoku {
@Test
public void test() throws IOException {
String filePath = "E:\\Project related\\demo\\sudoku.xlsx";
boolean isExcel2003 = true;
if (ExcelOperate.isExcel2007(filePath)) {
isExcel2003 = false;
}
File file = new File(filePath);
String[][] result = ExcelOperate.getData(file, 0, isExcel2003);
int rowLength = result.length;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < result[i].length; j++) {
System.out.print(result[i][j] + "|");
}
System.out.println("\t\t");
}
/** Convert the two-dimensional array read from excel to the two-dimensional data in the form of int, and the unknown number is represented by 0 */
int[][] arrays = new int[9][9];
for (int i = 0; i < 9; i ++) {
for (int j = 0; j < 9; j++) {
if (result[i][j] == null || "".equals(result[i][j])) {
arrays[i][j] = 0;
} else {
arrays[i][j] = Integer.parseInt(result[i][j]);
}
}
}
System.out.println("-----------------------------------------------");
arrays = calculate(arrays);
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
System.out.print(arrays[i][j] + "|");
}
System.out.println("\t\t");
}
System.out.println("success");
}
/**
* solving process
* @param arrays
* @return
*/
public static int[][] calculate(int[][] arrays) {
int count = 0; //Step number
//Get the existing digital location
int[][] indexs = getIndex(arrays);
for (int i = 0; i < 9; i ++) {
for (int j = 0; j < 9; j ++) { //Traverse the entire array
if (indexs[i][j] != -1) { //If it's an unknown number
//judge
do {
arrays[i][j] ++;
count++;
} while (!(compareRow(arrays, i, j) && compareCol(arrays, i, j) && compareLattice(arrays, i, j)));
if (arrays[i][j] > 9) {
if (indexs[i][j] != -1) {
arrays[i][j] = 0;
while (j != 0 && indexs[i][j-1] == -1) {
j = j - 1;
}
}
j = j - 2; //Step back
}
} else {
continue;
}
if (j < -1) {
i = i - 1;
j = 8;
while (indexs[i][j] == -1) {
j = j - 1;
}
j --;
}
}
}
System.out.println(count);
return arrays;
}
/**
* Compare with the number in this line
* @param arrays array
* @param row Row of location
* @param col Column in
* @return false Repeat, true not repeat
*/
public static boolean compareRow(int[][] arrays, int row, int col) {
for (int i = col + 1; i < 9; i ++) {
if (arrays[row][col] == arrays[row][i]) {
return false;
}
}
for (int i = col - 1; i >= 0; i --) {
if (arrays[row][col] == arrays[row][i]) {
return false;
}
}
return true;
}
/**
* Compare with the number in this column
* @param arrays array
* @param row Row of location
* @param col Column in
* @return false Repeat, true not repeat
*/
public static boolean compareCol(int[][] arrays, int row, int col) {
for (int i = row + 1; i < 9; i ++) {
if (arrays[row][col] == arrays[i][col]) {
return false;
}
}
for (int i = row - 1; i >= 0; i --) {
if (arrays[row][col] == arrays[i][col]) {
return false;
}
}
return true;
}
/**
* Compare whether it is the same as the number in the nine palace grid
* @param arrays array
* @param row Row of location
* @param col Column in
* @return false Repeat, true not repeat
*/
public static boolean compareLattice(int[][] arrays, int row, int col) {
int temp = 0;
for (int k = 0; k <= 8; k += 3) {
for (int l = 0; l <= 8; l += 3) {
if (row >= k && row <= k + 2 && col >= l && col <= l + 2) {
for (int i = k; i <= k + 2; i ++) {
for (int j = l; j <= l + 2; j ++) {
if (arrays[row][col] == arrays[i][j]) {
temp ++; //There can only be one repetition (and myself)
}
}
}
}
}
}
if (temp == 1) {
return true;
}else {
return false;
}
}
/**
* Get and where numbers exist
* @param arrays
* @return If a value of - 1 in a position means that it exists
*/
public static int[][] getIndex(int[][] arrays) {
int[][] temp = new int[9][9];
for (int i = 0; i < 9; i ++) {
for (int j = 0; j < 9; j ++) {
if (arrays[i][j] != 0) {
temp[i][j] = -1;
}
}
}
return temp;
}
}