我试图编写一个检查数独谜题是否有唯一解决方案的方法,如果为真,则返回带有空格的谜题来解决它。
这是生成它的类:
public class Sudoku {
private SudokuGen sudoku;
private boolean isUnique;
public Sudoku(){
this.isUnique = false;
while (!this.isUnique){
this.setSudoku(createUniqueSolution());
}
this.sudoku.getMyboard().showSudokuMatrix();
}
private SudokuGen createUniqueSolution(){
SudokuGen auxSudoku = new SudokuGen();
this.setSudoku(auxSudoku);
this.sudoku.getMyboard().showSudokuMatrix();
Position availableMove = auxSudoku.getFirstAvailableMove();
while (availableMove != null){
int row,col;
row = availableMove.getX();
col = availableMove.getY();
Integer value = auxSudoku.getValidvalue(row, col, 0);
auxSudoku.makeMove(row, col, value);
auxSudoku.loadValidvalues();
availableMove = auxSudoku.getFirstAvailableMove();
}
if (auxSudoku.getSolved()){
System.out.println("Unique solution");
this.isUnique = true;
return this.getSudoku();
}
else{
System.out.println("Solution is not unique");
return null;
}
}
public void setSudoku(SudokuGen sudoku){
this.sudoku = sudoku;
}
public SudokuGen getSudoku(){
return (this.sudoku);
}
}
SudokuGen会返回一个生成的拼图,我不知道解决方案是否唯一。所以我调用createUniqueSolution来检查它。
首先它创建auxSudoku并保存它,然后它开始使用auxSudoku拼图检查解决方案是否是唯一的。
然后我让下一个候选人再次调用availableMove,直到它为空。
所以简而言之,我的问题是如果我使用辅助变量修改this.sudoku的原因。提前谢谢。
这里是代码的输出:
212 238 0 0 175 372 311 0 103
0 0 0 238 0 103 312 372 0
0 372 0 0 212 0 238 175 0
0 312 0 0 372 311 212 10 0
372 0 10 0 238 0 103 312 0
0 212 0 0 0 0 372 311 0
311 0 0 372 10 175 0 0 312
312 0 238 311 0 212 175 0 0
24 0 372 312 0 0 10 0 0
Solution is not unique
0 100 49 71 185 0 0 262 178
0 71 262 0 300 49 0 100 257
0 0 177 100 262 0 71 0 0
0 185 300 262 71 100 178 0 0
0 0 0 177 49 0 0 0 0
0 0 178 300 257 185 0 71 0
0 262 0 185 0 300 0 178 0
0 177 0 0 178 71 0 185 0
71 0 185 0 0 0 49 0 300
Solution is not unique
284 0 0 0 0 193 25 179 0
32 25 193 179 284 0 0 0 0
0 0 0 92 25 0 296 0 284
0 32 296 0 0 0 0 0 179
193 0 0 0 92 0 32 296 84
0 179 284 0 296 371 193 0 92
0 284 32 0 371 0 0 92 0
296 92 371 25 179 0 0 0 193
179 0 84 296 0 0 371 0 0
独特的解决方案
284 296 92 371 84 193 25 179 32
32 25 193 179 284 296 92 84 371
371 84 179 92 25 32 296 193 284
92 32 296 84 193 25 284 371 179
193 371 25 284 92 179 32 296 84
84 179 284 32 296 371 193 25 92
25 284 32 193 371 84 179 92 296
296 92 371 25 179 284 84 32 193
179 193 84 296 32 92 371 284 25
最后一个输出由构造函数打印。
更新了课程
public class Sudoku {
private SudokuGen sudoku;
public Sudoku(){
SudokuGen auxSudoku = new SudokuGen();
while (!createUniqueSolution(auxSudoku)){
auxSudoku = new SudokuGen();
}
this.sudoku.getMyboard().showSudokuMatrix();
}
private boolean createUniqueSolution(SudokuGen sudoku){
this.sudoku = new SudokuGen(sudoku);
this.sudoku.getMyboard().showSudokuMatrix();
Position availableMove = sudoku.getFirstAvailableMove();
while (availableMove != null){
int row,col;
row = availableMove.getX();
col = availableMove.getY();
Integer value = sudoku.getValidvalue(row, col, 0);
sudoku.makeMove(row, col, value);
sudoku.loadValidvalues();
availableMove = sudoku.getFirstAvailableMove();
}
if (sudoku.getSolved()){
System.out.println("Unique solution");
return true;
}
else{
System.out.println("Solution is not unique");
return false;
}
}
public void setSudoku(SudokuGen sudoku){
this.sudoku = sudoku;
}
public SudokuGen getSudoku(){
return (this.sudoku);
}
}
SudokuGen的复制构造函数:
public SudokuGen(SudokuGen sudoku){
this.genboard = sudoku.genboard;
this.lockedCells = sudoku.lockedCells;
this.mutableCells = sudoku.mutableCells;
this.mutable = sudoku.mutable;
this.myboard = sudoku.myboard;
this.Solved = sudoku.Solved;
this.validvalues = sudoku.validvalues;
}
答案 0 :(得分:0)
方法setSudoku()不会创建SudokuGen对象的副本,而是将现有对象附加到父对象。
您想要的是为类SudokuGen创建一个复制构造函数,以便您可以将行this.sudoku = sudoku;
更改为this.sudoku = new SudokuGen (sudoku);