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 .
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.