Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C使用蛮力实现解决方案数独';行不通_C - Fatal编程技术网

C使用蛮力实现解决方案数独';行不通

C使用蛮力实现解决方案数独';行不通,c,C,我编写了用C实现暴力的代码,但没有运行 我试图调试它,但我得到了一个调试器无限循环,我需要帮助 int bruteForce(s_cellBoard[9][9],int i,int arr_32[]{ 如果(i==81 | | i

我编写了用C实现暴力的代码,但没有运行

我试图调试它,但我得到了一个调试器无限循环,我需要帮助

int bruteForce(s_cellBoard[9][9],int i,int arr_32[]{
如果(i==81 | | i<0)
返回1;
//如果他把电话号码放进牢房
if(可能(板、i、arr_32))
{//检查非givan的单元格-可以更改
do{i++;}while(board[i/9][i%9].isGiven);
}
其他的
{
板[i/9][i%9]。值=0;
//检查电池tht是否不合格-可以更换
do{i--;}while(board[i/9][i%9].isGiven);
}
返回蛮力(板,i,arr_32);
}
此函数如果可以在单元格中输入数字

int是可能的(s_cellBoard[9][9],int i,int arr_32[]
{

int num=board[i/9][i%9]。值?board[i/9][i%9]。值+1:1;
对于(;num以下对
bruteForce
函数的修改应该可以工作。我不确定
arr_32[]
参数的用途,因为它似乎没有被使用

int bruteForce(s_cellBoard board[9][9], int i, int arr_32[]) {
    // skip cells with 'given' numbers
    while (i < 81 && board[i / 9][i % 9].isGiven)
        i++;
    if (i == 81)
        return 1; // done all the cells, so solution found
    // check numbers 1 to 9 for current cell
    while (isPossible(board, i, arr_32))
    {
        // found a possible number for the cell
        // so check remaining cells
        if (bruteForce(board, i + 1, arr_32))
            return 1; // solution found
    }
    // tried all the numbers for this cell without finding a solution
    board[i / 9][i % 9].value = 0; // reset the cell
    return 0; // solution not yet found
}
int bruteForce(s_cellBoard[9][9],int i,int arr_32[]{
//跳过带有“给定”数字的单元格
而(i<81&&board[i/9][i%9].isGiven)
i++;
如果(i==81)
返回1;//完成所有单元格,因此找到了解决方案
//检查当前单元格的数字1到9
而(可能(董事会、i、arr_32))
{
//找到单元格的可能编号
//所以检查剩余的细胞
if(蛮力(板,i+1,arr_32))
返回1;//找到解决方案
}
//尝试了此单元格的所有数字,但未找到解决方案
线路板[i/9][i%9]。值=0;//重置单元
返回0;//尚未找到解决方案
}

在那些
do…while
循环中,
i
有可能超出范围,因此您应该检查:
do{i++}while(i<81&&board[i/9][i%9]。给定;
do{i--}while(i>=0&&board[i/9][i%9].isGiven;
。如何跟踪在每个位置尝试了哪些数字?我猜
bruteForce
最初是从
main
调用的,并且
I
参数设置为0。在这种情况下,如果
board[0][0]。isGiven
为真,则
isPossible
函数将在“给定”上调用单元格。这看起来像一个bug。在
isPossible
返回false后,
bruteForce
返回轨迹,但您的代码中似乎没有任何东西可以阻止它以与上次完全相同的数字开始,因此它将永远不会朝解决方案前进。int num=board[i/9][i%9]。value?board[i/9][i%9].value+1:1;输入num this value+1,使其从下一个数字开始,但这仍然是一个无止境的过程loop@user11674988这对我来说很有效。如果您的
行()
列()
板()
函数是错误的。您能在问题中显示这些函数的代码吗?@user11674988我已经更正了
isGiven
成员的名称,以匹配您的代码,但这是您在上面的评论之后我所做的全部更改。
int col(s_cellBoard board[9][9], int i, int num) {
    int j;
    for (j = 0; j < 9; j++)
    {
        if (board[j][i % 9].value == num)
            return 0;
    }
    return 1;
}
int row(s_cellBoard board[9][9], int i, int num) {
    int j;
    for (j = 0; j < 9; j++)
    {
        if (board[i / 9][j].value == num)
            return 0;
    }
    return 1;
}
int bruteForce(s_cellBoard board[9][9], int i, int arr_32[]) {
    // skip cells with 'given' numbers
    while (i < 81 && board[i / 9][i % 9].isGiven)
        i++;
    if (i == 81)
        return 1; // done all the cells, so solution found
    // check numbers 1 to 9 for current cell
    while (isPossible(board, i, arr_32))
    {
        // found a possible number for the cell
        // so check remaining cells
        if (bruteForce(board, i + 1, arr_32))
            return 1; // solution found
    }
    // tried all the numbers for this cell without finding a solution
    board[i / 9][i % 9].value = 0; // reset the cell
    return 0; // solution not yet found
}