数独算法,蛮力[关闭](Sudoku algorithm, brute force [closed]

2019-06-23 18:16发布

荫试图解决与蛮力算法数独板,我真的不能正确得到这个算法的工作。

有被创建为包含所有平方(细胞)每一行,列和框对象属于实际柱,正方形和行,这是在使用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;
}

Answer 1:

boolean setNumberMeAndTheRest(Board board) {

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

然后每次返回false时,您还需要将板复位到其原始状态

board = original;
return false;


文章来源: Sudoku algorithm, brute force [closed]