只能算初级的..高级的就溢出了
就算内存无穷大.可能性超过了20亿就数组放不下了
因为是广度优先吧..所以..争取能写个深度优先的办法
哎..好难啊..头发掉了好多
package com.ansj.ansjIndex;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class CopyOfShuDu {
public static void main(String[] args) {
int[][] ints = {{4,7,0,6,0,0,0,0,0},
{0,0,1,9,4,0,0,0,0},
{0,9,6,5,1,7,2,4,0},
{0,0,2,0,9,0,0,3,0},
{0,0,0,1,3,2,0,0,0},
{0,8,0,0,5,0,6,0,0},
{0,3,9,2,8,5,7,6,0},
{0,0,0,0,6,1,3,0,0},
{0,0,0,0,0,9,0,5,8}};
add(ints);
}
public static void add(int[][] ints) {
int[][][] intsTemp = new int[9][9][0];
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints[i].length; j++) {
if (ints[i][j] == 0) {
int[] tempInt = havePosb(ints, i, j) ;
if(tempInt==null){
add(ints) ;
return ;
}
intsTemp[i][j] = tempInt;
}
}
}
if(isOk(ints)){
for (int i = 0; i < ints.length; i++) {
printArray(ints[i]) ;
}
System.out.println("--------------------------------------------------------");
return ;
}
zuhe(ints, intsTemp);
for (int i = 0; i < list.size(); i++) {
fillVacancy(ints,list.get(i)) ;
if(isOk(ints)){
for (int j = 0; j < ints.length; j++) {
printArray(ints[j]) ;
}
System.out.println("--------------------------------------------------------");
}else{
}
}
}
public static void fillVacancy(int[][] ints , int[] temp){
int count = 0 ;
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints[i].length ; j++) {
if(ints[i][j]<=0){
ints[i][j] = temp[count]*-1 ;
count++ ;
}
}
}
}
//广度优先排列组合
public static void zuhe(int[][] ints, int[][][] intsTemp) {
List<int[]> listArray = new ArrayList<int[]>() ;
for (int i = 0; i < intsTemp.length; i++) {
for (int j = 0; j < intsTemp[i].length; j++) {
if(intsTemp[i][j].length>0){
listArray.add(intsTemp[i][j]) ;
}
}
}
addHead(listArray) ;
}
private static LinkedList<int[]> list= new LinkedList<int[]>();
private static int pamerLength ;
public static void addHead(List<int[]> listArray){
int[] temp = listArray.get(0) ;
for (int i = 0; i < temp.length; i++) {
list.add(Arrays.copyOfRange( temp,i , i+1)) ;
}
pamerLength = listArray.size() ;
addHeap(listArray) ;
}
public static void addHeap(List<int[]> listArray){
int innerLength = 1;
while (true) {
int[] temp = list.pollFirst();
innerLength = temp.length ;
// 深度够了则执行完成
if (innerLength == pamerLength) {
list.addFirst(temp);
return;
} else {
for (int innerInt : listArray.get(innerLength)) {
// 依次入队列的末端
int[] newTemp = new int[innerLength+1] ;
for (int i = 0; i < temp.length; i++) {
newTemp[i] = temp[i] ;
}
newTemp[temp.length] = innerInt ;
list.addLast(newTemp);
}
}
}
}
/**
* 找出数组中空白处可以填写的数字
*
* @param ints
* @param i
* @param j
*/
public static int[] havePosb(int[][] ints, int i, int j) {
int[] arr = new int[10];
for (int k = 0; k < 9; k++) {
arr[Math.abs(ints[k][j])] = 1;
}
for (int k = 0; k < 9; k++) {
arr[Math.abs(ints[i][k])] = 1;
}
int n = i / 3;
int m = j / 3;
for (int k = 1; k < 3; k++) {
arr[Math.abs(ints[n * 3 + k][m * 3 + k])] = 1;
}
List<Integer> all = new ArrayList<Integer>();
for (int k = 1; k < arr.length; k++) {
if (arr[k] == 0) {
all.add(k);
}
}
int[] tempInt = new int[all.size()];
for (int k = 0; k < tempInt.length; k++) {
tempInt[k] = all.get(k);
}
if(tempInt.length==1){
ints[i][j] = tempInt[0] ;
tempInt = null ;
}
return tempInt;
}
public static boolean isOk(int[][] ints) {
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints.length; j++) {
if (ints[i][j] == 0) {
return false;
}
}
}
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints.length; j++) {
if(ints[i][j]>0){
continue ;
}
// for (int j1 = 0; j1 < ints.length; j1++) {
// printArray(ints[j1]) ;
// }
// System.out.println("--------------------------------------------------------");
int[] arr = new int[9];
for (int k = 0; k < 9; k++) {
arr[Math.abs(ints[i][k])-1]++ ;
}
for (int k = 0; k < 9; k++) {
arr[Math.abs(ints[k][j])-1]++ ;
}
// printArray(arr) ;
for (int k = 0; k < arr.length; k++) {
if(arr[k]!=2)return false ;
}
}
}
return true;
}
/**
* 重置数组
*
* @param ints
*/
private static void resetArray(int[][] ints) {
for (int i = 0; i < ints.length; i++) {
for (int j = 0; j < ints[i].length; j++) {
if (ints[i][j] < 0) {
ints[i][j] = 0;
}
}
}
}
private static void printArray(int[] ints) {
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i] + " ");
}
System.out.println();
}
}