简体   繁体   English

C中的数独求解器无法正常工作

[英]Sudoku solver in C not working properly

I've written this Sudoku solver in C. But it isn't working properly, Any help? 我已经用C编写了这个Sudoku求解器。但是它不能正常工作,有帮助吗?

With a sample input such as 带有示例输入,例如

1 0 3 4 0 0 7 0 9
0 5 6 0 8 9 0 2 3
0 8 9 1 0 3 4 0 6
2 1 4 0 6 5 0 9 7
3 0 0 8 0 7 0 1 4
8 0 7 0 1 4 0 6 5
0 3 1 0 4 0 9 7 8
6 4 0 9 7 0 5 3 1
0 7 8 0 0 1 0 4 2

it gives output 它给出输出

1 2 3 4 5 6 7 8 9 
7 5 6 0 8 9 1 2 3 
0 8 9 1 2 3 4 5 6 
2 1 4 3 6 5 8 9 7 
3 9 5 8 0 7 2 1 4 
8 0 7 2 1 4 3 6 5 
5 3 1 6 4 2 9 7 8 
6 4 2 9 7 8 5 3 1 
9 7 8 5 3 1 6 4 2 

Any idea what went wrong? 知道出了什么问题吗?

#include<stdio.h>

int sudoku[9][9];

int check(int sudoku[][9], int row, int col, int sol)
{
    //checking in the grid
    int row_grid = (row/3) * 3;
    int col_grid = (col/3) * 3;

    int i, j;
    for(i=0; i<9; ++i)
    {
        if (sudoku[row][i] == sol)                             
            return 0;
        if (sudoku[i][col] == sol)                             
            return 0;
        if (sudoku[row_grid + (i%3)][col_grid + (i/3)] == sol) 
            return 0;
    }
    return 1;
}  


int main(void)
{
    int i,j,k;
    printf("enter the sudoku and enter 0 for unknown entries \n");
    for(i=0;i<9;i++)
    {
        for (j=0;j<9;j++)
        {
            scanf("%d",&sudoku[i][j]);
        }
    }    
    for(i=0;i<9;i++)
    {
        for (j=0;j<9;j++)
        {
            if(sudoku[i][j]==0)
            {
                for (k=1;k<=9;k++)
                {
                    if(check(sudoku,i,j,k)==1)
                    {
                        sudoku[i][j] = k;                   
                    }
                }               
            }
        }
    }

    printf("solved sudoku \n");
    for(i=0;i<9;i++)
    {
        for (j=0;j<9;j++)
        {
            printf("%d ", sudoku[i][j]);
        }
        printf("\n");       
    }
    return 0;
}

You aren't going to solve a sudoku this way. 您不会以这种方式解决数独问题。 You just loop through all cells and for each empty cell you pick the biggest value that can be there. 您只需遍历所有单元格,并为每个空单元格选择可能存在的最大值。 Obviously, it is incorrect, since there might be several variants in one cell. 显然,这是不正确的,因为一个单元格中可能有多个变体。 And you always pick the biggest (since there is no break in a for (k=...) loop). 并且您总是选择最大的(因为for (k=...)循环中没有break )。 Sometimes you pick the wrong number and it makes a sudoku inconsistent. 有时您选择了错误的数字,从而使数独不一致。

UPDATE: what you do is not a brute-force, since you don't iterate through all possible solutions. 更新:您所做的不是蛮力的,因为您不会遍历所有可能的解决方案。 Instead of 代替

if(check(sudoku,i,j,k)==1)
{
    sudoku[i][j] = k;                   
}

There must be something like 一定有类似的东西

if(check(sudoku,i,j,k)==1)
{
    tempSudoku = copy(sudoku); //pseudocode
    tempSudoku[i][j] = k;
    tryToSolveRecursively(tempSudoku);              
}

If check() returned 0, then there is an inconsistency (for some empty cell we can't find a single possible solution). 如果check()返回0,则存在不一致(对于某些空单元格,我们找不到单个可能的解决方案)。 And so we should abandon this solution, go back and try something else (viva la recursion :) ). 因此,我们应该放弃此解决方案,然后回去尝试其他方法(通过递归:))。 This is not an only option, but I just hope that it will point you in a right direction. 这不是唯一的选择,但我只是希望它将为您指明正确的方向。

PS Brute force solution based just on basic sudoku rules will be terribly slow . 仅基于基本数独规则的PS Brute Force解决方案将非常缓慢 Trust me. 相信我。 You need additional heuristics to reduce the search space. 您需要其他试探法以减少搜索空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM