可能重复:
数独算法,蛮力
几天来,我一直在尝试编写一个蛮力算法来解决数独问题,我的问题是我从来没有真正让算法 100% 工作,有人可以指导我并提供一些帮助吗?
该算法位于 Square 类,递归函数。
public abstract class Square {
private Square next;
private Box box;
private Row row;
private Columne columne;
private int value;
Square(int value, Box box, Row row, Columne columne) {
this.value = value;
this.box = box;
this.row = row;
this.columne = columne;
}
void setNumberMeAndTheRest(Board board) {
if(getNext() == null) {
System.out.println("next == null");
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
}
}
board.saveSolution();
return;
} else {
if(this instanceof DefinedSquare) {
getNext().setNumberMeAndTheRest(board);
} else {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
getNext().setNumberMeAndTheRest(board);
}
}
return;
}
}
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
void setNext(Square next) {
this.next = next;
}
public Square getNext() {
return next;
}
/**
* Checks if value is legal in box, row and column.
* @param value to check.
* @return true if value is legal, else false.
*/
boolean legalValue(int value) {
if(box.legalValue(value) && row.legalValue(value) && columne.legalValue(value)) {
return true;
}
return false;
}