小编Car*_*n U的帖子

Java中的数独求解器,使用回溯和递归

我正在用Java编写一个用于9x9网格的数独求解器.

我有方法:

  • 打印网格

  • 用给定的值初始化电路板

  • 测试冲突(如果相同的数字在同一行或3x3子网格中)

  • 一种逐个放置数字的方法,这需要最多的工作.

在我详细介绍该方法之前,请记住我必须使用递归来解决它,以及回溯(在这里观看applet作为示例http://www.heimetli.ch/ffh/simplifiedsudoku.html)

此外,我正在通过垂直向下移动来解决这个数独,从左上角开始,到第一列,然后到第二列,等等.

到目前为止,我有以下内容:

public boolean placeNumber(int column){

    if (column == SUDOKU_SIZE){  // we have went through all the columns, game is over

        return true;

    }

    else
    {
        int row=0;  //takes you to the top of the row each time

        while (row < SUDOKU_SIZE)    loops through the column downwards, one by one
        {

            if (puzzle[row][column]==0){  //skips any entries already in there (the given values)

                puzzle[row][column]=1;   //starts with one

                while(conflictsTest(row,column)){   //conflictsTest is the method …
Run Code Online (Sandbox Code Playgroud)

java recursion sudoku backtracking

24
推荐指数
1
解决办法
2万
查看次数

标签 统计

backtracking ×1

java ×1

recursion ×1

sudoku ×1