I am trying to learn backtracing , so i wrote program for Sudoku It solves sudoku in approx. 1 ms in online c compiler
but this one sudoku puzzle is taking approx. 35 sec in that online c compiler with my code.
How to improvise the code , does recursion will help ? .If yes how ?(because recursion will also take same number of iterations as my code). I tried c codes available on google also (Sudoku Solver in C) with recursion , this one is taking approx. 7sec. to solve.
input:
0 5 6 0 0 0 0 0 0
0 0 0 7 0 0 8 0 0
0 0 0 9 0 0 0 0 0
7 1 0 0 0 0 0 0 0
0 0 4 0 0 0 5 0 0
0 0 0 0 0 0 3 0 6
2 0 0 0 6 0 0 9 0
0 0 0 0 5 3 0 0 0
0 0 0 0 0 0 0 1 0
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define pass 1
#define fail 0
#define forward 1
#define backward 0
int flag = 0 ;
int a[9][9] = {0} ;
int rulecheck_row(int row,int a[9][9])
{
int i=0,j=0;
for(i=0;i<9;i++)
{
for(j=i+1;j<9;j++)
{
if((a[row][i] !=0) || ( a[row][j] !=0 ))
{
if(a[row][i] == a[row][j])
{
return fail;
}
}
}
}
return pass;
}
int rulecheck_column(int column,int a[9][9])
{
int i=0,j=0;
for(i=0;i<9;i++)
{
for(j=i+1;j<9;j++)
{
if((a[i][column] !=0) || (a[j][column]))
{
if(a[i][column] == a[j][column])
return fail;
}
}
}
return pass;
}
int duplicate_checker(int arr[9])
{
int length = 9 ;
for(int i = 0; i < length; i++) {
for(int j = i + 1; j < length; j++) {
if(arr[i]!=0 || arr[j]!=0)
{
if(arr[i] == arr[j])
return 1 ;
}
}
}
return 0;
}
int repeat[3][3]={0};
int rulecheck_3x3matrix(int a[9][9])
{
int b[9];
int k=0;
for(int loopr = 0 ; loopr < 3 ;loopr++)
{
for(int loop=0;loop<3;loop++)
{
for(int row=loopr*3;row<((loopr+1)*3);row++)
{
for(int col=loop*3;col<((loop+1)*3);col++)
{
b[k++] = a[row][col] ;
}
}
repeat[loopr][loop] = duplicate_checker(b);
k=0;
}
}
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(repeat[i][j] == 1)
return fail;
}
}
return pass;
}
int isPresentInBox(int i, int j, int num){
int boxStartRow = i - i%3 ;
int boxStartCol = j - j%3 ;
//check whether num is present in 3x3 box or not
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
if((row+boxStartRow) == i && (col+boxStartCol) ==j)
{
continue ;
}
if (a[row+boxStartRow][col+boxStartCol] == num)
return fail;
}
}
return pass;
}
int rulecheck_allelements()
{
int i=0,j=0;
// All elements should be b/w 0-9
for( i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j] < 1 || a[i][j] > 9)
return fail ;
}
}
return pass;
}
int main()
{
int numofalreadyfilled = 0 ;
int row[81],column[81] ;
int flag_occupied = 0;
int x = 0;
int number = 0;
int number_backup = 0;
int direction = forward ;
int flag_break_whileloop = 0;
clock_t start, end;
double cpu_time_used;
printf("Enter numbers with 0 for empty cells with space: \n");
for(int l=0;l<9;l++)
{
for(int m=0;m<9;m++)
{
scanf("%d",&a[l][m]);
}
}
printf("\n --- Entry finished ---\n");
start = clock();
for(int n=0;n<9;n++)
{
for(int o=0;o<9;o++)
{
if(a[n][o] >0 )
{
numofalreadyfilled++ ;
row[x] = n;
column[x] = o;
x++ ;
}
}
}
printf("\nx:%d alrea : %d\n",x,numofalreadyfilled);
printf("\n\n");
for(int n=0;n<9;n++)
{
printf("\n");
for(int o=0;o<9;o++)
{
printf(" %d ",a[n][o]);
}
}
for(int l=0;l<9;l++)
{
if(rulecheck_row(l,a) == fail)
{
printf("\n identical numbers in row\n");
exit(0);
}
if(rulecheck_column(l,a) == fail)
{
printf("\n identical numbers in column\n");
exit(0);
}
}
if(rulecheck_3x3matrix(a) == fail)
{
printf("\n identical numbers in box\n");
exit(0);
}
printf("\n");
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
for(int x=0;x<numofalreadyfilled;x++)
{
if((i == row[x]) && (j == column[x]))
{
x = 0;
flag_occupied = 1;
break;
}
}
if(flag_occupied == 1)
{
flag_occupied = 0 ;
if(direction == forward)
continue ;
else //backtracking direction:backward
{
j--;
j--;
if(j<-1 && i>0)
{
i-- ;
j=7;
}
continue ;
}
}
if(direction == backward)
{
a[i][j]++ ;
if(a[i][j]>9)
{
a[i][j] = 0;
j--;
j--;
if(j<-1 && i>0)
{
i-- ;
j=7;
}
continue ;
}
}
while(a[i][j] <= 9)
{
if(a[i][j] == 0)
{
a[i][j] = 1;
}
if(rulecheck_row(i,a) & rulecheck_column(j,a) & isPresentInBox(i,j, a[i][j]))
{
direction = forward ;
flag_break_whileloop = 1;
break;
}
else
{
a[i][j]++ ;
if(a[i][j]>9)
{
a[i][j] = 0;
j--;
j--;
if(j<-1 && i>0)
{
i-- ;
j=7;
}
direction = backward ;
flag_break_whileloop = 1;
break;
}
}
}
flag_break_whileloop = 0;
//printf("---[%d][%d] num=%d \n",i,j,a[i][j]);
if(j<-3)
{
printf("\n No Solution");
exit(0);
}
}
}
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
// printf("Hello World");
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("\ntime : %lf",cpu_time_used);
return 0;
}