How to find the first solution only with this backtracking

前端 未结 2 1857
遥遥无期
遥遥无期 2021-01-03 17:13

i\'m trying to write a Sudoku solver which will return only the first possible solution. i managed to print all possible solutions with void methods but i can\'t stop on the

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 17:36

    Change

            if(board1.boardIsOk())           // check if the board is legal
                return nextCell(line, column); // continue
    

    into

            if(board1.boardIsOk()) {          // check if the board is legal
                boolean solved = nextCell(line, column); // continue
                if (solved) {
                    return true;
                ]
            }
        ...
        return false;
    

提交回复
热议问题