我正在用 Java 制作一个 Sudoku Solver 程序,它使用回溯和蛮力来解决算法。我似乎遇到的问题是,当我在一个简单的容器中插入正确的值时,当我尝试从另一个类访问它时会显示错误的值。
当我从插入方法内部打印出 HashMap 时,HashMap 在正确的位置具有正确的值Sudokucontainer
,(使用 a for(int i = 0 i < dim; i++) + for(int j = 0; j < dim; j++)
)后跟Square[][] tmp = solutionsHash.get(count)
and System.out.println(tmp[i][j].getValue());
。当我在类中的solutions.insert
语句之后使用相同的 for 循环时,它也是成功的(然后是相同的语句。Square
fillInnRemainingOfBoard()
Square[][] tmp = solutions.solutionsHash.get(count)
System.out.println
当我尝试从Board
类中执行相同操作时会出现问题,正如您在该类中的 test() 方法中看到的那样。然后它只打印出预定义的值(如果板为空,则不打印)。唯一的区别是我再用ieSquare[][] tmp = allSquares[0][0].solutionsHash.get(wantedNumber)
和以前一样。但正如我所提到的,我不明白为什么在-classsolutionsHash
中声明为静态时会有所作为。Sudokucontainer
非常感谢任何帮助。谢谢!
注意:压痕不知何故搞砸了,对此感到抱歉。我没有包含 GUI 部分,因为我没有写过它,而且我可能没有权限把它放在这里。问题也发生在此之前(可能在Board
课堂上),因为在那里打印出与Board
课堂上相同的值。
编辑:删除了一些不必要的代码。
public class Square {
static Sudokucontainer solutions;
protected char value;
protected int valueInt;
protected static Square allSquares[][];
public int count = 0;
protected boolean predefined = false;
protected boolean lastSquare = false;
protected Square next = null;
public void setNext() {
if(col.getNr() < col.dim) {
next = allSquares[row.getNr()-1][col.getNr()];
}else if(col.getNr() == col.dim) {
if(row.getNr() < row.dim) {
next = allSquares[row.getNr()][0];
}else {
next = null;
lastSquare = true;
}
}
if(next != null) {
if(next.col.getNr() == col.dim && next.row.getNr() == col.dim && next.predefined == true)
lastSquare = true;
}
}
public void fillInnRemainingOfBoard(Square[][] allSquares, int hd, int br) {
this.allSquares = allSquares;
int highestNumber = col.dim;
setNext();
if(value == '\u0020') {
for(int i = 1; i <= highestNumber; i++) {
if(row.isLegal[i-1] && col.isLegal[i-1] && box.isLegal[i-1]) {
String s1 = Integer.toString(i);
value = s1.charAt(0);
insert(value);
insertInt(i);
box.isLegal[i-1] = false;
col.isLegal[i-1] = false;
row.isLegal[i-1] = false;
if(!lastSquare) {
next.fillInnRemainingOfBoard(this.allSquares, hd, br);
}else{
System.out.println("Solution found: " + count);
solutions.insert(allSquares, count, col.dim);
++count;
} // Slutt paa else
box.isLegal[valueInt-1] = true;
col.isLegal[valueInt-1] = true;
row.isLegal[valueInt-1] = true;
value = '\u0020';
} // Slutt paa if(row.isLegal[i-1] && col.isLegal[i-1] && box.isLegal[i-1])
} // Slutt paa if (value == '\u0020' || !predefined)
}else {
if(!lastSquare) {
next.fillInnRemainingOfBoard(this.allSquares, hd, br);
} else {
System.out.println("Solution found");
solutions.insert(allSquares, count, col.dim);
count++;
}
} // Slutt paa else if(predefined)
// Slutt paa for(int i = 1; i <= highestNumber; i++)
}// Her returnerer vi til forrige metode
public void insert(char value) {
if(value != '.') {
this.value = value;
}else{
this.value = '\u0020'; // In other words; space(in unicode)
}
}
public void insertInt(int value) {
valueInt = value;
}
public char getValue() {
return value;
}
}
public class Sudokucontainer {
int dim = 0;
private int count = 0;
static HashMap<Integer, Square[][]> solutionsHash = new HashMap<Integer, Square[][]>();
public void insert(Square[][] allSquares, int count, int dim) {
this.count = count;
this.dim = dim;
if(count <= 499) {
solutionsHash.put(count,allSquares);
}
}
public Square[][] get(int nr) {
Square[][] tmp = solutionsHash.get(nr);
return tmp;
}
public int getSolutionCount() {
return solutionsHash.size();
}
}
public class Board {
int dim, br, hd;
char[][] charArray;
char[] charArray1;
static Square [][] allSquares;
Row [] rows;
Column[] columns;
Box[] boxes;
public Board(int dim, int br, int hd, char[][] charArray) {
this.dim = dim;
this.br = br;
this.hd = hd;
this.charArray = charArray;
charArray1 = new char[dim];
allSquares = new Square[dim][dim];
rows = new Row[dim];
columns = new Column[dim];
boxes = new Box[dim/br * dim/hd];
setRows();
setColumns();
setBoxes();
setSquares();
insertBoxInSquares();
fillInLegalValues();
welcome();
solve();
test();
System.out.println("Found solutions. Please refer to the graphical interface.");
showGui();
}
public void test() {
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
//Square[][] tmp = allSquares[0][0].solutions.solutionsHash.get(0);
//System.out.println(tmp[i][j].getValue());
}
}
}
public void solve() {
allSquares[0][0].fillInnRemainingOfBoard(allSquares, hd, br);
}
public void showGui() {
new SudokuGUI(dim, hd, br, allSquares[0][0].solutions.solutions, false, 0, allSquares[0][0].solutions.solutionsHash);
}
}