数独算法,蛮力

时间:2012-04-18 19:43:58

标签: algorithm sudoku brute-force

我试图用强力算法解决数独板,我真的无法使这个算法正常工作。

为每个行,列和框创建了一个对象,其中包含属于实际列,方形和行的所有正方形(单元格),这在legalValue()中用于检查是否可以将值放入单元格中

我找不到使算法起作用的结构。

    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;
}

这是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

添加了重置电路板

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;
}

经过很长一段时间后,她是一个解决方案: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;
}

1 个答案:

答案 0 :(得分:1)

boolean setNumberMeAndTheRest(Board board) {

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

然后每次返回false时,您还需要将电路板重置为原始状态

board = original;
return false;