Hey there. I wrote a program that solves Sudoku puzzles but I am not able to execute it properly. The program does not respond after I've fed it input;i.e, the program does not proceed.
Here's the code:
#include<stdio.h>
int rowChk(int grid[][9],int row,int val)
{
int x;
for(x=0;x<9;x++)
if(grid[row][x]==val)
return 0;
return 1;
}
int colChk(int grid[][9],int col,int val)
{
int x;
for(x=0;x<9;x++)
if(grid[x][col]==val)
return 0;
return 1;
}
int boxChk(int grid[][9],int row,int col,int val)
{
row=(row/3)*3;
col=(col/3)*3;
for(;row<(row+3);row++)
for(;col<(col+3);col++)
if(grid[row][col]==val)
return 0;
return 1;
}
void solve(int grid[][9])
{
int i,j,ch1,ch2,ch3,k,status;
k=1;
status=0;
for(i=0;i<9;i++)
{ for(j=0;j<9;j++)
{
if(grid[i][j]==0 || !status)
{
if(k>9)
k=k%9;
grid[i][j]=k++;
}
ch1=rowChk(grid,i,grid[i][j]);
ch2=colChk(grid,j,grid[i][j]);
ch3=boxChk(grid,i,j,grid[i][j]);
if(ch1&&ch2&&ch3)
status=1;
else
j--;
}
}
}
int main()
{
int i,j,grid[10][10];
printf("\n....Sudoku Solver....\n");
printf("Enter the matrix:\n");
for(i=0;i<9;i++)
for(j=0;j<9;j++)
scanf("%d",&grid[i][j]);
solve(grid);
printf("\nSolved puzzle:\n");
for(i=0;i<9;i++)
{ for(j=0;j<9;j++)
printf("%d ", grid[i][j]);
printf("\n");
}
return 0;
}
I tried to identify the bug using gdb...apparently the execution halts at solve() function.