c++ - 数独递归回溯,反递归太早

标签 c++ recursion sudoku backtracking

所以我正在用 C++ 编写数独解算器,但遇到了一些小问题。下面是我的解决板代码。它适用于拼图的前 3 行,但在到达第 4 行末尾时不再递归。查看 gdb 上的代码,它到达第 4 行的末尾,回溯到第 6 列,尝试然后反递归到最后。

关于代码的其他一些注意事项是保存数独板的矩阵从 1,1 而不是 0,0 开始。因此,当最初调用 solveBoard 时,参数为 (1, 1, 0)。我还附上了 setCell 和 checkConflicts 函数以获得更多信息。我有三个 vector rowConf、colConf 和 squConf 来存储已经放置在相应行、列或正方形中的值。我已经在这几个小时了,无法让它超过第三排。非常感谢任何帮助。谢谢!

编辑:添加了 clearCell()

bool board::solveBoard(int i, int j, int count)
{

    if (j > 9)
    {
        j = 1;
        i++;

        printBoard();
        if (isSolved())
        {
            printBoard();
            cout <<"The Board has been solved!" <<endl
                 <<" The number of recursive calls was: " <<count <<endl;
            return true;
        }
     }

     if (isBlank(i, j))
     {
         for (int n = 1; n < 10; n++)
         {
             if (setCell(i, j, (char)n + '0'))
             {
                 if (solveBoard(i, j + 1, count + 1))
                 {
                     return true;
                 }
              }
          }
      }
      else
      {
          return (solveBoard(i, j + 1, count + 1));
      }

      clearCell(i, j);
      return false;
}

bool board::setCell(int i, int j, char val)
{
    int intVal;

    intVal = atoi(&val);

    if (i >= 1 && i <= BoardSize && j >= 1 && j <= BoardSize &&
        intVal >= 1 && intVal <= BoardSize)
    {
        if (!(checkConflicts(intVal, i, j, squareNumber(i, j))))
        {
        return false;
        }

        value[i][j] = intVal;

        // Set flags of the conflicts
        rowConf[i][intVal] = true;
        colConf[j][intVal] = true;
        squConf[squareNumber(i, j)][intVal] = true;

        return true;
    }
    else
    {
        throw rangeError("bad value in setCell");
    }
}

bool board::checkConflicts(int val, int i, int j, int k)
{
    if (i < 1 && i > BoardSize && j < 1 && j > BoardSize &&
        k < 1 && k > BoardSize && val < 1 && val > BoardSize)
    {
        throw rangeError("bad value in checkConflicts()");
    }

    if (rowConf[i][val] || colConf[j][val] || squConf[k][val])
    {
        return false;
    }
    else
    {
        return true;
    }
}

Initial Board:
 -----------------------------
| 3       |    8    |          -----------------------------
|         | 7       |       5  -----------------------------
| 1       |         |          ----------------------------- 
 -----------------------------
|         |         | 3  6     -----------------------------
|       2 |       4 |          -----------------------------
|    7    |         |          -----------------------------
 -----------------------------
|         |    6    | 1  3     -----------------------------
|    4  5 | 2       |          -----------------------------
|         |         | 8        -----------------------------
 -----------------------------

Final Output:
 -----------------------------
| 3  2  4 | 1  8  5 | 6  7  9  -----------------------------
| 6  8  9 | 7  2  3 | 4  1  5  -----------------------------
| 1  5  7 | 4  9  6 | 2  8  3  -----------------------------
 -----------------------------
|         |         | 3  6     -----------------------------
|       2 |       4 |          -----------------------------
|    7    |         |          -----------------------------
 -----------------------------
|         |    6    | 1  3     -----------------------------
|    4  5 | 2       |          -----------------------------
|         |         | 8        -----------------------------
 -----------------------------

void board::clearCell(int i, int j)
{
    int intVal;

    if (i >= 1 && i <= BoardSize && j >= 1 && j <= BoardSize)
    {
        if (value[i][j] != -1)
        {
            intVal = value[i][j];
            rowConf[i][intVal] = false;
            colConf[j][intVal] = false;
            squConf[squareNumber(i, j)][intVal] = false;
            value[i][j] = -1;
         }
    }
    else
    {
        throw rangeError("bad value in setCell");
    }
}

最佳答案

您的问题很可能在这里:

if (isBlank(i, j))
 {
     for (int n = 1; n < 10; n++)
     {
         if (setCell(i, j, (char)n + '0'))
         {
             if (solveBoard(i, j + 1, count + 1))
             {
                 return true;
             }
          }
      }
  }

它以某种方式通过了这个部分,这就是为什么它最终没有通过 else,但由于之前没有返回,它被卡住了。

这需要更多的调试,但这里有一个可能导致解决方案的想法:

if (isBlank(i, j))
{
    for (int n = 1; n < 10; n++)
    {
        if (setCell(i, j, (char)n + '0'))
        {
            if (solveBoard(i, j + 1, count + 1))
            {
                return true;
            } else {
                echo 'Looks like it ended on the farthest-level..';
          } 
      } else {
          echo 'Looks like it ended on the second-farthest level.';
      }
  }

关于c++ - 数独递归回溯,反递归太早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13215823/

相关文章:

c++ - 数独解算器由于某种原因不断卡住

c++ - 数组、对象、类来组织具有多项选择和真/假问题的测试问卷程序

c++ - 解释器始终可以使用源代码是什么意思?

c++ - vector 作为图的边

java - 在我的案例中,我可以在不循环遍历所有前面元素的情况下计算一个元素吗(参见问题正文)?

algorithm - 数独与 MPI 并行化

c++ - 设计建议 : llvm multiple runtime contexts

c - 在这个简单的例子中,递归是如何工作的?

python - 通过实现堆排序的递归按值或引用传递

java - CPLEX 中的数独求解器