I'm new to C++ and have to do a home assignment (sudoku). I'm stuck on a problem.
Problem is that to implement a search function which to solve a sudoku.
Instruction: In order to find a solution recursive search is used as follows. Suppose that there is a not yet assigned field with digits (d1....dn) (n > 1). Then we first try to assign the field to d1, perform propagation, and then continue with search recursively. What can happen is that propagation results in failure (a field becomes empty). In that case search fails and needs to try different digits for one of the fields. As search is recursive, a next digit for the field considered last is tried. If none of the digits lead to a solution, search fails again. This in turn will lead to trying a different digit from the previous field, and so on.
数字dにフィールドを割り当てて試行する前に、現在のボードのコピーである新しいボードを作成する必要があります(コピーコンストラクターを使用して、ヒープからボードをnewで割り当てます)。その後、コピーに対して割り当てを実行します。検索への再帰呼び出しが失敗した場合は、次の桁を試すために新しいボードを作成できます。
私はもう試した:
// Search for a solution, returns NULL if no solution found
Board* Board::search(void) {
// create a copy of the cur. board
Board *copyBoard = new Board(*this);
Board b = *copyBoard;
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
// if the field has not been assigned, assign it with a digit
if(!b.fs[i][j].assigned()){
digit d = 1;
// try another digit if failed to assign (1 to 9)
while (d <=9 && b.assign(i, j, d) == false){
d++;
// if no digit matches, here is the problem, how to
// get back to the previous field and try another digit?
// and return null if there's no pervious field
if(d == 10){
...
return NULL;
}
}
}
}
return copyBoard;
}
もう1つの問題は、再帰呼び出しをどこで使用するかです。任意のヒント?どうも!
完全な手順はここにあります:http ://www.kth.se/polopoly_fs/1.136980!/Menu/general/column-content/attachment/2-2.pdf
コード:http ://www.kth.se/polopoly_fs/1.136981!/Menu/general/column-content/attachment/2-2.zip