2

I'm trying to implement a code of (SolveSudoku) by using backtracking. But I always get an error.

I looked at many solutions in the internet ,but they are different which confuses me. that's why I'm asking here. I think the mistake can be in the function is_safe somewhere ,because other people solved it by using reference/dereference (pointers/adresses).

this is my code :

#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;
}

I always get -1073741819 as output .

Alaa Alsayed
  • 133
  • 8

2 Answers2

1

You have included one header twice:

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

and omitted

#include <stdio.h>

Even when corrected, there are several compiler warnings, four like

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

and one is

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

and also two similar to

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

My compilation when run does not give a wrong answer: it crashes. So fix all the warnings.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • mmmm No there is (else) for every (if) it means. by the way we will get something back. – Alaa Alsayed Feb 11 '19 at 23:28
  • 1
    "Not all control paths return a value" is given because there is no *unconditional* `return` statement. The compiler does not execute the code to find out, and as another answer says, it is in the wrong place. You should set the warning level to maximum and fix them all. – Weather Vane Feb 11 '19 at 23:29
1

The function

    bool check_Row(int **puzzle,int number,int column)
    {
        for(int row=0;row<9;row++)
        {
            if(puzzle[row][column]==number)
            {
                return true;
            }
            else{return false;}
        }
    }

only tests the 0th row. No matter what number is there it returns immediately. Ditto for other checks.

user58697
  • 7,808
  • 1
  • 14
  • 28
  • yes if the number exists ,than returns true otherwise false and that's why there is a parameter colmun and a loop for(row...) – Alaa Alsayed Feb 11 '19 at 23:25
  • 1
    @Amerov Read carefully: it only tests one row. It doesn't loop. – user58697 Feb 11 '19 at 23:34
  • @Amerov The `else{return false;}` to be removed and `return false` set after the `for` loop ... repeat the fix to other functions (not saying the whole algo is correct though) – Déjà vu Feb 11 '19 at 23:37
  • @Amerov because there are lots of errors. Please don't "fix the code" you posted, fix the code you have on your machine. – Weather Vane Feb 11 '19 at 23:52