</pre><pre name="code" class="java">/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test;
import java.util.*;
/**
*
* @author Administrator
*/
public class Sudoku {
public static class Coord {
public int x;
public int y;
Coord() {
x = -1;
y = -1;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
public static class Cell {
public boolean isProcessed;
public List validList;
public int value;
public int pro_num;
Cell() {
isProcessed = false;
validList = null;
value = -1;
pro_num =0;
}
public void pickValidValue() {
//获取随机数
Random random = new Random();
int result = random.nextInt(this.validList.size());
this.value = (Integer) this.validList.get(result);
}
public void clear() {
this.isProcessed = false;
this.validList = null;
this.value = -1;
}
public boolean getIsIsProcessed() {
return isProcessed;
}
public void setIsProcessed(boolean isProcessed) {
this.isProcessed = isProcessed;
}
public List getValidList() {
return validList;
}
public void setValidList(List validList) {
this.validList = validList;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getPro_num() {
return pro_num;
}
public void setPro_num(int pro_num) {
this.pro_num = pro_num;
}
}
public static void main(String[] args) {
Coord coCurrent = new Coord();
coCurrent.setX(0);
coCurrent.setY(0);
int m_size = 9;
Random random = new Random();
Cell[][] m_cells = new Cell[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
m_cells[i][j] = new Cell();
}
}
while (true) {
System.out.println("x="+coCurrent.getX()+"y="+coCurrent.getY());
Cell c = m_cells[coCurrent.getX()][coCurrent.getY()];
System.out.println("c.getIsIsProcessed()="+c.getIsIsProcessed());
List al;
//如果该格子还未填值,获取可取的值
if (!c.getIsIsProcessed()) {
al = getValidValueList(coCurrent, m_cells);
c.setValidList(al);
System.out.println("list"+c.getValidList());
}else{
int pro_num =c.getPro_num()+1;
System.out.println("回溯次数——"+pro_num);
System.out.println("list"+c.getValidList());
c.setPro_num(pro_num);
if(pro_num == c.getValidList().size()){
c.setIsProcessed(false);
c.setValidList(null);
c.setValue(-1);
c.setPro_num(0);
coCurrent = prevCoord(coCurrent);
System.out.println("我要回溯到x="+coCurrent.getX()+"y="+coCurrent.getY());
continue;
}
}
//如果有可选值
if (c.getValidList() != null) {
//c.pickValidValue(); //选一个填进去
if (c.getValidList().size() > 1) {
int result = random.nextInt(c.getValidList().size());
c.setValue((Integer) c.getValidList().get(result));
}else{
c.setValue((Integer) c.getValidList().get(0));
}
c.setIsProcessed(true);
if (coCurrent.getX() == m_size - 1 && coCurrent.getY() == m_size - 1) {
break;
} else {
coCurrent = nextCoord(coCurrent);
}
} else {//如果没有,回溯
//如果当前是(0,0),结束while循环
if (coCurrent.getX() == 0 && coCurrent.getY() == 0) {
break;
} else {
c.setIsProcessed(false);
c.setValidList(null);
c.setValue(-1);
c.setPro_num(0);
coCurrent = prevCoord(coCurrent);
System.out.println("我要回溯到x="+coCurrent.getX()+"y="+coCurrent.getY());
}
}
}
for (int i = 0; i < m_size; i++) {
for (int j = 0; j < m_size; j++) {
if (j != 8) {
System.out.print(m_cells[i][j].getValue()+" ");
} else {
System.out.println(m_cells[i][j].getValue());
}
}
}
}
//获取可行解
private static List getValidValueList(Coord coCurrent, Cell[][] m_cells) {
int x = coCurrent.getX();
int y = coCurrent.getY();
List list = new ArrayList();
for (int i = 1; i < 10; i++) {
boolean flag_x = true;
boolean flag_y = true;
boolean flag = true;
int num = i; //如果这个地方放的值为num
//判断同一行是否有相同的值
for (int h = 0; h < 9; h++) {
if (h != y && m_cells[x][h].getValue() == num) {
flag_y = false;
}
}
//判断同一列是否有相同的值
for (int l = 0; l < 9; l++) {
if (l != x && m_cells[l][y].getValue() == num) {
flag_x = false;
}
}
//判断同一个3*3的格内有没有重复的
int n = 0;
int m = 0;
if (x % 3 == 0) {
n = x;
} else if (x % 3 == 1) {
n = x - 1;
} else {
n = x - 2;
}
if (y % 3 == 0) {
m = y;
} else if (y % 3 == 1) {
m = y - 1;
} else {
m = y - 2;
}
for (int nn = n; nn < n + 3; nn++) {
for (int mm = m; mm < m + 3; mm++) {
if (nn != x && mm != y && m_cells[nn][mm].getValue() == num) {
flag = false;
}
}
}
if (flag_x && flag_y && flag) {
list.add(num);
}
}
if(list.size()>0){
return list;
}else{
return null;
}
}
private static Coord prevCoord(Coord coCurrent) {
int x = coCurrent.getX();
int y = coCurrent.getY();
if (y != 0) {
y = y - 1;
} else {
y = 8;
x = x - 1;
}
coCurrent.setX(x);
coCurrent.setY(y);
return coCurrent;
}
private static Coord nextCoord(Coord coCurrent) {
int x = coCurrent.getX();
int y = coCurrent.getY();
if (y != 8) {
y = y + 1;
} else {
y = 0;
x = x + 1;
}
coCurrent.setX(x);
coCurrent.setY(y);
return coCurrent;
}
}