1. Sudoku Validation. The input to this component is a 4x4 matrix representing a
ID: 3750323 • Letter: 1
Question
1. Sudoku Validation. The input to this component is a 4x4 matrix representing a completed 4x4 Sudoku. The input is provided in the variable matrix'. The programmer needs to verify the validity of the configuration and store the logical answer in the 'valid' variable. For example, If the board configuration is valid (i.e. satisfies the rules of Sudoku) then logical value of 1 is stored in the valid variable. The rules for a given matrix to be a valid solution are: a. Every row of the matrix must consist of numbers from 1 to 4. (The given input will consist of 4 rows) Every column of the matrix must consist of numbers from 1 to 4. (The given input will consist of 4 columns) Every sub-matrix of size 2x2 must consist of numbers from 1 to 4. (The given input will consist of 4 2x2 sub-matrices). b. c.Explanation / Answer
following is the c++ code of the above problem with self expainatory comments
--------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
//function to verify submatrix and rows and columns with sudoku rules.
int isSubMatrixValid(int arr[])
{
for(int i=0;i<3;i++)
{
for(int j = i+1 ;j < 4; j ++)
{
if((arr[i] == arr[j] ) || arr[i] < 1 || arr[i] > 4 || arr[j] < 1 || arr[j] > 4)
{
return 0;
}
}
}
return 1;
}
int main() {
// input matrix
int matrix[4][4];
// valid to represent the verification of sudoku matrix
int valid = 1;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cin >> matrix[i][j];
}
}
int testarr[4] = {0};
// testing rows
for(int i=0;i<4;i++)
{
testarr[0] = matrix[i][0];
testarr[1] = matrix[i][1];
testarr[2] = matrix[i][2];
testarr[3] = matrix[i][3];
int res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
}
// testing columns
for(int i=0;i<4;i++)
{
testarr[0] = matrix[0][i];
testarr[1] = matrix[1][i];
testarr[2] = matrix[2][i];
testarr[3] = matrix[3][i];
int res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
}
//testing sub matrix
int a = 0, b = 0;
// first sub matrix
testarr[0] = matrix[a][b];
testarr[1] = matrix[a+1][b];
testarr[2] = matrix[a][b+1];
testarr[3] = matrix[a+1][b+1];
int res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
// second sub matrix
a = 2; b = 0;
testarr[0] = matrix[a][b];
testarr[1] = matrix[a+1][b];
testarr[2] = matrix[a][b+1];
testarr[3] = matrix[a+1][b+1];
res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
// third sub matrix
a = 0; b = 2;
testarr[0] = matrix[a][b];
testarr[1] = matrix[a+1][b];
testarr[2] = matrix[a][b+1];
testarr[3] = matrix[a+1][b+1];
res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
// fourth sub matrix
a = 2; b = 2;
testarr[0] = matrix[a][b];
testarr[1] = matrix[a+1][b];
testarr[2] = matrix[a][b+1];
testarr[3] = matrix[a+1][b+1];
res = isSubMatrixValid(testarr);
if(res == 0)
{
valid = 0;
cout << valid;
return 0;
}
// all conditions satified so sudoku is valid
valid = 1;
cout << valid;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.