以下全部我有一個9x9數獨求解器的大綱,但我不確定如何將多個解決方案併入部分條目的某個數獨(如果尚未存在的話)。有人可以通過這個運行我嗎?數獨求解器多種解決方案
該算法採用回溯(因此,使用堆棧)
Algorithm findSolutions:
Given:
(cell, value) findDecidableCell(puzzle) - returns reference to a cell (if any) whose value
can be immediately decided along that value
void Puzzle::decide(cell, value) - note that value has been decided for cell
bool Puzzle::solved() - return true if the puzzle has been solved
Input:
puzzle - ADT representing the set of possible solutions to current puzzle
strategies[] - list of deductive strategies
Returns:
list of solutions
list<Puzzle> solutions
stack<Puzzle> alternatives // holds alternate outcomes of speculative simplifications
alternatives.push(puzzle) // our start state is our first alternative
while(!alternatives.empty()) { // more solutions possible
puzzle = alternatives.pop()
// decide all immediately decidable cells
while((cell, value) = findDecidableCell(puzzle)) {
puzzle.decide(cell, value)
}
// try simplification strategies until we hit a dead end or solution
simplificationFound = true
while(!puzzle.solved() && simplificationFound) {
// try deductive strategies
simplificationFound = false
for(i = 0; i < strategies.length && !simplificationFound; ++i) {
simplificationFound = strategies[i].simplify(&puzzle)
}
// fall back to guessing
if(!simplificationFound) {
Puzzle alternative;
if(simplificationFound = guess(&puzzle, &alternative)) {
// guess may be wrong, record alternate outcome
alternatives.push(alternative);
}
}
// decide all immediately decidable cells before looking for
// further simplifications
if(simplificationFound) {
while((cell, value) = findDecidableCell(puzzle)) {
puzzle.decide(cell, value)
}
}
}
// We either found a solution or a contradiction (no simplifications)
if(puzzle.solved()) {
solutions.push_back(puzzle);
}
}
是所有的僞代碼?因爲你錯過了很多分號 – mpen