Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ - solve a sudoku game
    primarykey
    data
    text
    <p>I'm new to C++ and have to do a home assignment (sudoku). I'm stuck on a problem.</p> <p>Problem is that to implement a search function which to solve a sudoku. </p> <p>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.</p> <p>Before a digit d is tried by assigning a field to it, you have to create a new board being a copy of the current board (use the copy constructor and allocate the board from the heap with new). Only then perform the assignment on the copy. If the recursive call to search returns unsuccessfully, a new board can be created for the next digit to be tried.</p> <p>I've tried:</p> <pre><code>// 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 &lt; 9; i++){ for(int j = 0; j &lt; 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 &lt;=9 &amp;&amp; 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; } </code></pre> <p>Another problem is where to use the recursive call? Any tips? thx!</p> <p>Complete instruction can been found here: <a href="http://www.kth.se/polopoly_fs/1.136980!/Menu/general/column-content/attachment/2-2.pdf" rel="nofollow">http://www.kth.se/polopoly_fs/1.136980!/Menu/general/column-content/attachment/2-2.pdf</a></p> <p>Code: <a href="http://www.kth.se/polopoly_fs/1.136981!/Menu/general/column-content/attachment/2-2.zip" rel="nofollow">http://www.kth.se/polopoly_fs/1.136981!/Menu/general/column-content/attachment/2-2.zip</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload