下面我有一个 9x9 Sudoku Solver 的大纲,但我不确定如何将多个解决方案与部分条目合并到某个 Sudoku 中,如果还没有的话。有人可以帮我解决这个问题吗?
该算法使用回溯(因此,使用堆栈)
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);
}
}