[英]Error by solving sudoku using backtracking
I'm trying to implement a code of (SolveSudoku) by using backtracking.我正在尝试通过使用回溯来实现 (SolveSudoku) 的代码。 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).
我认为错误可能在函数 is_safe 的某个地方,因为其他人通过使用引用/取消引用(指针/地址)解决了它。
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 .我总是得到 -1073741819 作为输出。
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
警告 C4715:“check_Column”:并非所有控制路径都返回值
and one is一个是
warning C4024: 'is_safe': different types for formal and actual parameter 1
警告 C4024:“is_safe”:形式参数和实际参数 1 的类型不同
and also two similar to还有两个类似的
warning C4047: '=': 'int *' differs in levels of indirection from 'int **'
警告 C4047:“=”:“int *”与“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.只测试第 0 行。 No matter what number is there it returns immediately.
无论那里有什么数字,它都会立即返回。 Ditto for other checks.
其他检查同上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.