-2

Iam trying to solve a sudoku board with a brute force algorithm, I cant really get this algorithm work correctly.

There is created a object for each row, column and box that contains all squares(cells) that belongs to the actually column, square and row, this is used in legalValue() to check if value can be placed in the cell.

I cant find the structure that make the algorithm to work.

    boolean setNumberMeAndTheRest(Board board) {


    if(getNext() == null) {
        for(int i = 1; i <= board.getDimension(); i++) {
            if(legalValue(i)) {
                setValue(i);
            }
        }
        board.saveSolution();
    } else {
        if(this instanceof DefinedSquare) {
            getNext().setNumberMeAndTheRest(board);

        } else {
            for(int i = 1; i <= board.getDimension(); i++) {
                if(legalValue(i)) {
                    setValue(i);

                    if(getNext().setNumberMeAndTheRest(board)) {
                        return true;
                    } else {
                        setValue(i);
                    }
                }
            }
            return false;
        }
    }

    return false;
}

Here is legalValue(int i);

/**
 * Checks if value is legal in box, row and column.
 * @param value to check.
 * @return true if value is legal, else false.
 */
boolean legalValue(int value) {
    if(box.legalValue(value) && row.legalValue(value) && columne.legalValue(value)) {
        return true;
    }
    return false;
}

**4x4 Sudoku board INPUT**

0 2 | 1 3
0 0 | 0 4
---------
0 0 | 0 1
0 4 | 3 2

**Expected OUTPUT**

4 2 | 1 3
3 1 | 2 4
---------
2 3 | 4 1
1 4 | 3 2

**Actually OUTPUT**

4 2 | 1 3
2 4 | 3 4
---------
3 0 | 0 1
0 4 | 3 2

Added resetting of board

boolean setNumberMeAndTheRest(Board board) {

    Board original = board;

    if(getNext() == null) { 
        for(int i = 1; i <= board.getDimension(); i++) {
            if(legalValue(i)) {
                setValue(i);
            }
        }
        board.saveSolution();

    } else {
        if(this instanceof DefinedSquare) {
            getNext().setNumberMeAndTheRest(board);

        } else {
            for(int i = 1; i <= board.getDimension(); i++) {
                if(legalValue(i)) {
                    setValue(i);

                    if(getNext().setNumberMeAndTheRest(board)) {
                        return true;
                    } else {
                        setValue(i);
                    }
                }
            }
            board = original;
            return false;
        }
    }
    board = original;
    return false;
}

Her is a solution, after a long time :D

boolean setNumberMeAndTheRest(Board board) {

    if(next == null) {
        board.saveSolution();
        return true;
    }

    if(this instanceof DefinedSquare) {
        return next.setNumberMeAndTheRest(board);
    }

    for(int i = 1; i <= board.getDimension(); ++i) {
        if(legalValue(i)) {
            setValue(i);

            if(next.setNumberMeAndTheRest(board)) {
                return true;
            }
        }
    }
    setValue(0);
    return false;
}
user265767
  • 559
  • 3
  • 12
  • 27
  • 1
    what do you mean you can't get it to work? What's the error? – twain249 Apr 18 '12 at 19:47
  • The output is like this, there is zeroes in the last row.561733 242567 624364 265642 756350 300102 – user265767 Apr 18 '12 at 19:52
  • a. comment your code so we can tell what you're doing b. provide sample inputs, expected outputs, and actual outputs c. use a 3x3 or 4x4 to test with, much easier to trace than 6x6 – Jason Apr 18 '12 at 20:20
  • Ok, Thanks. Have done b and c, some a. – user265767 Apr 18 '12 at 20:32
  • 1
    You need to unset the value before returning `false`. (You tried to fill the cell, you failed, leave the cell exactly as you've got it). Also, the "object-oriented" approach doesn't buy anything here except complications. A simple 2D array of numbers will do the job just fine. – n. m. could be an AI Apr 18 '12 at 20:33
  • I now see that legalValue(int value) dosent check the box, thats now corrected, actually the same output. I do not understand where or how to unset the value, can you please show me ? – user265767 Apr 18 '12 at 20:43
  • possible duplicate of [Brute force Sudoku algorithm](http://stackoverflow.com/questions/10193330/brute-force-sudoku-algorithm) – andrew cooke Apr 18 '12 at 20:49
  • Yep, but no more answers on the other – user265767 Apr 18 '12 at 20:53
  • What operation removes a number from the cell? How should I know? You have never used such operation, nor exposed the internals of your class. – n. m. could be an AI Apr 18 '12 at 21:28
  • How come you get a **5** in a 4×4 sudoku? – n. m. could be an AI Apr 18 '12 at 21:31
  • You seem to use `this instanceof DefinedSquare` as a test for an empty square. I wonder how the result of this check ever changes. – n. m. could be an AI Apr 18 '12 at 21:33
  • 5 is typos, corrected. I does not have a function or something outside this algorithm for changing values. I asked this question because I dont know what I do, or how to write the algorithm, I'm confused :( – user265767 Apr 18 '12 at 21:42
  • I final did find a solution, see my edit. – user265767 Apr 19 '12 at 13:57

1 Answers1

1
boolean setNumberMeAndTheRest(Board board) {

  // make a copy of the original board
  Board original = board;

then every time you return false, you also need to reset the board to its original state

board = original;
return false;
Jason
  • 86,222
  • 15
  • 131
  • 146