gpt4 book ai didi

java - 蛮力数独算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:30 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Sudoku algorithm, brute force

几天来我一直在尝试编写一个强力算法来解决数独问题,我的问题是我从来没有真正让算法 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;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com