gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:23:55 24 4
gpt4 key购买 nike

所以我正在用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com