I came across this question online stated being asked in some company’s test. It’s like this:
You are given a 9x9 grid, a filled sudoku but there are some error in it, there may be max 3 digits in-correctly filled in the sudoku grid (i.e.there may be 1 , 2, or 3 errors in sudoku filling). You have to device a computer program that detects those errors in the sudoku and places the correct digits(1-9) at those wrong filled cells of the sudoku.
Output should be in the form : place in the grid where there is an error in the sudoku and the correct digit to be placed there to make the sudoku correct.
I could not find any such kind of solution online, so asking out for help here. Please someone help how to solve such a problem.
For each ith row and jth column check whether it clears all the constraints
- if Sudoku[i][j] is unique (1-9) in the ith row
- if Sudoku[i][j] is unique (1-9) in the jth column
- if in the Block (3 * 3) in which (i,j) exists ,Sudoku[i][j] is unique (1-9)
Constraints function is given below
if it returns 0 (it means that in the present state of Sudoku it’s Correct) move to next (i,j)
if it returns 1 (it means that is the incorrect value in sudoku) try out all possible values (1-9) that clears the Constraint (using backtracking)
and write your own Sudoku function that somewhat looks like Soduku ( board,i,j )
that does the remaining task as I have stated. if you need help just write it in comments.
int Constraints(int N,int i,int j)
{
int value = board[i][j],k,l,m = i,n = j;
for(k = 0;k < 9;k++)
if(board[k][j] == value)
{
if(k == i)
continue;
printf("Constraint 1 failed (Column %d)\n",j);
return 1;
}
for(k = 0;k < 9;k++)
if(board[i][k] == value)
{
if(k == j)
continue;
printf("Constraint 2 failed (Row %d)\n",i);
return 1;
}
i = i / 3;
j = j / 3;
for(k = 0;k < 3 ;k++)
for(l = 0;l < 3;l++)
if(board[3*i + k][3*j +l] == value)
{
if(3*i + k == m && 3*j +l == n)
continue;
printf("Constraint 3 failed (Block %d)\n",3*i + j);
return 1;
}
return 0;
}
HAPPY CODING