Question

I am writing a recursive solver for a Sudoku puzzle. I store blank places with 0's so that my program can read them more easily. I have the original puzzle stored starting from subscript one in order to better visualize the grid. I am not sure I have a full grasp on the recursion and that is where the problem is. I am getting output that seems like it is on track to solve the puzzle, but it is leaving zeros there that shouldn't be there. I think it has something to do with the placement of my unsetSquare, or with the return statements.

Here is a sample of the output...

**************************************************
 7  4  3 | 8  2  1 | 5  6  8 
 2  6  8 | 0  9  0 | 0  1  0 
 0  0  0 | 0  0  6 | 0  0  4 
---------|---------|---------
 0  0  0 | 0  0  0 | 2  3  9 
 0  0  0 | 0  0  0 | 0  0  0 
 4  1  5 | 0  0  0 | 0  0  0 
---------|---------|---------
 9  0  0 | 5  0  0 | 0  0  0 
 0  2  0 | 0  1  0 | 7  4  0 
 0  0  0 | 2  0  0 | 9  0  5 
**************************************************
**************************************************
 7  4  3 | 8  2  1 | 5  6  9 
 2  6  8 | 0  9  0 | 0  1  0 
 0  0  0 | 0  0  6 | 0  0  4 
---------|---------|---------
 0  0  0 | 0  0  0 | 2  3  9 
 0  0  0 | 0  0  0 | 0  0  0 
 4  1  5 | 0  0  0 | 0  0  0 
---------|---------|---------
 9  0  0 | 5  0  0 | 0  0  0 
 0  2  0 | 0  1  0 | 7  4  0 
 0  0  0 | 2  0  0 | 9  0  5 
**************************************************
**************************************************
 7  4  3 | 8  2  1 | 5  6  0 
 2  6  8 | 1  9  0 | 0  1  0 
 0  0  0 | 0  0  6 | 0  0  4 
---------|---------|---------
 0  0  0 | 0  0  0 | 2  3  9 
 0  0  0 | 0  0  0 | 0  0  0 
 4  1  5 | 0  0  0 | 0  0  0 
---------|---------|---------
 9  0  0 | 5  0  0 | 0  0  0 
 0  2  0 | 0  1  0 | 7  4  0 
 0  0  0 | 2  0  0 | 9  0  5 
**************************************************
**************************************************
 7  4  3 | 8  2  1 | 5  6  0 
 2  6  8 | 2  9  0 | 0  1  0 
 0  0  0 | 0  0  6 | 0  0  4 
---------|---------|---------
 0  0  0 | 0  0  0 | 2  3  9 
 0  0  0 | 0  0  0 | 0  0  0 
 4  1  5 | 0  0  0 | 0  0  0 
---------|---------|---------
 9  0  0 | 5  0  0 | 0  0  0 
 0  2  0 | 0  1  0 | 7  4  0 
 0  0  0 | 2  0  0 | 9  0  5 
**************************************************
**************************************************
 7  4  3 | 8  2  1 | 5  6  0 
 2  6  8 | 3  9  0 | 0  1  0 
 0  0  0 | 0  0  6 | 0  0  4 
---------|---------|---------
 0  0  0 | 0  0  0 | 2  3  9 
 0  0  0 | 0  0  0 | 0  0  0 
 4  1  5 | 0  0  0 | 0  0  0 
---------|---------|---------
 9  0  0 | 5  0  0 | 0  0  0 
 0  2  0 | 0  1  0 | 7  4  0 
 0  0  0 | 2  0  0 | 9  0  5 
**************************************************

notice at the end of the first row, it goes to 8 looking for a solution, then to 9, 9 is not legal and it has reached the end of the for loop, so it replaces it with a zero and it continues. How can I make it go back to try different numbers in the first row to get a more complete solution?

Here is my recursion function...

bool DoTheWork::addSquare(int& depth, ostream& outStream){
    for(int i = ONE; i <= NINE; ++i){
        for(int j = ONE; j <= NINE; ++j){
            if(i == NINE && j == NINE && board.getSquare(NINE, NINE) != ZERO){
                cout << i << "     " << j << endl;
                return true;
            }
            //cout << "original" << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
            if(board.getSquare(i, j) == ZERO){
                //cout << "original: " << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
                for(int k = ONE; k <= NINE; ++k){
                    board.setSquare(i, j, k);
                    board.display(outStream);
                    if(board.isLegal()){
                         return addSquare(depth, outStream);  
                    }
                    else{
                        board.unsetSquare(i, j);

                    }
                }
            }

        }
    }
    board.display(outStream);
    return false;
}
Was it helpful?

Solution 2

It turns out I had somethings in the wrong order, and not in the right scope. After successfully doing it with a while loop, I found the solution with a for loop.

bool DoTheWork::addSquare(int& depth, ostream& outStream){
    for(int i = 1; i < 10; ++i){
        for(int j = 1; j < 10; ++j){
            if(board.getSquare(i, j) == 0){
                if(i == 10){
                    return true;
                }
                for(int k = 1; k <= 9; ++k){
                    board.setSquare(i, j, k);
                    if(board.isLegal() && addSquare(depth, outStream)){
                        return true;
                    }

                }
                board.unsetSquare(i, j);
                return false;

            }
        }
    }
}

OTHER TIPS

I can see a problem:

It should be:

if(board.isLegal()){
   if( addSquare(depth, outStream))
       return true;  
}

Means if the whole board is solved then roll back.

EDIT think about it:

you put 1 in the first square, it returns false. don't you want to try put 2?

EDIT2 More problem:

if(i == NINE && j == NINE && board.getSquare(NINE, NINE) != ZERO){
          cout << i << "     " << j << endl;
          return true;
}

Is this suppose to be a completion check? you check only 1 square. and in your sample it is filled! you need to check that there is no 0.

EDIT3:

bool DoTheWork::addSquare(int& depth, ostream& outStream){
  //use flag to let you know if all completed
  bool zeroFound = false;
  for(int i = ONE; i <= NINE; ++i){
    for(int j = ONE; j <= NINE; ++j){
      //cout << "original" << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
      if(board.getSquare(i, j) == ZERO){
        zeroFound = true;
        //cout << "original: " << board.getSquare(i, j) << "coord: " << i << ", " << j << endl;
        for(int k = ONE; k <= NINE; ++k){
          board.setSquare(i, j, k);
          board.display(outStream);
          if(board.isLegal()){
            if(addSquare(depth, outStream)){
              return true;  
            }
            else{
              board.unsetSquare(i, j);
            }
          }
        }
      }
    }
  } 
  board.display(outStream);
  return !zeroFound; //true in case it is full!
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top