c - 使用回溯解决数独错误

标签 c sudoku recursive-backtracking

我正在尝试使用回溯来实现(SolveSudoku)代码。 但我总是收到错误。

我在网上看了很多解决方案,但它们都是不同的,这让我很困惑。这就是我在这里问的原因。 我认为错误可能出现在函数 is_safe 的某个地方,因为其他人通过使用引用/取消引用(指针/地址)解决了它。

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 9
int ** create_puzzle()
{
    int i , j;
    int **my_puzzle;
    my_puzzle =(int**)malloc(sizeof(int*)*9);

    int my_array [N][N]={ 0,1,9,   0,0,2,   0,0,0,
                          4,7,0,   6,9,0,   0,0,1,
                          0,0,0,   4,0,0,   0,9,0,

                          8,9,4,   5,0,7,   0,0,0,
                          0,0,0,   0,0,0,   0,0,0,
                          0,0,0,   2,0,1,   9,5,8,

                          0,5,0,   0,0,6,   0,0,0,
                          4,7,0,   0,2,8,   0,7,9,
                          0,0,0,   1,0,0,   8,6,0};
    for (i =0;i<N;i++)
    {

        my_puzzle[i]=(int **)malloc(sizeof(int *)*9);
        for(j=0;j<N;j++)
        {
            my_puzzle [i][j]=my_array[i][j];

        }
    }
    return my_puzzle;
}
void print_puzzle(int **puzzle)
{
    int r,c;

    for(r=0;r<N;r++)
    {
    if(r%3==0){
            printf("-------------------------------\n");
                   }
        for(c=0;c<N;c++)
        {
            if(c%3==0)
            printf("|");
            printf("%d  ",puzzle[r][c]);

        }
        printf("| \n");
    }
    printf("-------------------------------\n");
}

//function to check if all cells are assigned or not
bool is_zero(int **puzzle,int row ,int column)
{
    if(puzzle[row][column]==0)
    {
        return true;
    }
    return false;
}
//checking in row
bool check_Row(int **puzzle,int number,int column)
{
    for(int row=0;row<9;row++)
    {
        if(puzzle[row][column]==number)
        {
            return true;
        }

    }
     return false;
}
//checking column
bool check_Column(int ** puzzle,int number,int row)
{
    for(int column=0;column<9;column++)
    {
        if(puzzle[row][column]==number)
        {
            return true;
        }
    }
    return false;
}
//checking sub matrix
bool check_box(int **puzzle,int number,int row,int column)
{
    int row_start=(row/3)*3;
    int start_column=(column/3)*3;

    for(int i=row_start;i<row_start+3;i++)
    {
        for(int j=start_column;j<start_column+3;j++)
        {

    if(puzzle[i][j]==number)
    {
        return true;
    }
        }
    }
   return false;
}
//function to check if we can put a
//value in a paticular cell or not
bool is_safe(int ** puzzle,int number,int row,int column)
{
    if(is_zero(puzzle,row ,column))
    {
        return !check_Row(puzzle,number,column)&&
               !check_Column(puzzle,number,row)&&
               !check_box(puzzle,number, row,column);
    }
    return false;
}
//function to solve sudoku
//using backtracking
bool sudoko_solver(int **puzzle)
{
    int row,column;
    int number=0;
    for(row=0;row<9;row++)
    {
        for(column=0;column<9;column++)
        {
            if(is_safe(puzzle[row][column],number,row,column))
            {
                puzzle[row][column]=number;
                if(sudoko_solver(puzzle))
                {
                    return true;
                }
                else
                {
                    puzzle[row][column]=0;
                }
            }
        }
    }
 return false;
}
int main ()
{
    int **puzzle= create_puzzle();
    print_puzzle(puzzle);
    if(sudoko_solver(puzzle))
    {
        print_puzzle(puzzle);
    }
    else
    {
        printf("No solution");
    }
    return 0;
}

我总是得到 -1073741819 作为输出。

最佳答案

您已将同一个 header 包含两次:

#include <stdlib.h>
#include <stdlib.h>

并省略

#include <stdio.h>

即使更正,也会出现几个编译器警告,例如四个

warning C4715: 'check_Column': not all control paths return a value

其中一个是

warning C4024: 'is_safe': different types for formal and actual parameter 1

还有两个类似的

warning C4047: '=': 'int *' differs in levels of indirection from 'int **'

我的编译在运行时没有给出错误的答案:它崩溃了。因此修复所有警告。

关于c - 使用回溯解决数独错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640519/

相关文章:

c - 递归回溯数独求解器

c++ - 仅由长度为 1 到 10 的两个字母生成所有字符串?

C *运算符在数组赋值中的含义

c - C 中的 Malloc 内存损坏

C : infinite loop in the input

javascript - 递归回溯生成迷宫

recursion - 用特定票据表示金额

c - 使用 OpenSSL 进行 DSA 签名

c++ - 数独求解器在某些游戏中挂起

c - 指向二维数组的指针作为c函数中的参数