1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#include <iostream>
const int N = 3;
const int SIDE = N * N;
void get_matrix(int matrix[][SIDE] );
bool row_ok(int matrix[][SIDE], int);
bool col_ok(int matrix[][SIDE], int);
bool sub_matrix_ok( int aMatrix[][SIDE], int, int);
int main()
{
//HARD CODE FOR TESTING
int matrix[][SIDE] =
{
{9,8,5,2,4,3,1,6,7},
{3,2,8,7,8,1,9,5,4},
{7,4,1,9,1,5,3,8,2},
{4,5,2,8,7,9,6,3,1},
{1,9,3,6,5,4,1,7,8},
{6,7,8,1,3,2,5,4,9},
{5,3,9,4,1,7,8,2,6},
{8,1,7,5,2,6,4,5,3},
{2,6,4,3,9,8,7,1,5}
};
//CHECK EACH ROW
std::cout << "CHECK ROWS:\n";
for (int row = 0; row < SIDE; row++)
std::cout << "Row " << row << ": " << row_ok(matrix, row ) << '\n';
//CHECK EACH COLUMN
std::cout << "CHECK COLUMNS:\n";
for (int col = 0; col < SIDE; col++)
std::cout << "Col " << col << ": " << col_ok(matrix, col ) << '\n';
// CHECK EACH SUB-MATRIX
std::cout << "CHECK SUB-MATRICES:\n";
for (int R = 0 ; R < N; R++)
{
for(int C = 0; C < N; C++)
std::cout << "Sub-matrix [" << R << "][" << C << "]: " << sub_matrix_ok( matrix, R, C) << '\n';
}
return 0;
}
// CHECK A SUB_MATRIX
bool sub_matrix_ok( int aMatrix[][SIDE], int R, int C)
{
int digit[9] = {0};
for(int row = N * R; row < N * (R + 1); row++)
{
for(int col = N * C; col < N * (C + 1); col++)
digit[ aMatrix[row][col] - 1 ] = 1;
}
for (int i = 0; i < 9; i++)
{
if(digit[i] == 0)
return false;
}
return true;
}
// GET MATRIX VALUES - STILL NEEDS TO BE FULLY TESTED
void get_matrix(int matrix[SIDE][SIDE])
{
int temp = 0;
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++ )
{
while ( std::cout << "Please enter a value: " and std::cin >> temp and temp > 0 and temp <= SIDE)
{
matrix[i][j] = temp;
std::cout << "matrix[" << i << "[[" << j << "] = " << temp << '\n';
}
}
}
}
// EACH ROW MEMBER MUST BE DIFFERENT (ALREADY BETWEEN 1 & 9)
bool row_ok(int matrix[][SIDE], int row)
{
int digit[9] = {0};
for(int col = 0; col < SIDE; col++)
digit[ matrix[row][col] - 1 ] = 1;
for (int i = 0; i < 9; i++)
{
if(digit[i] == 0)
return false;
}
return true;
}
// EACH COL MEMBER MUST BE DIFFERENT (ALREADY BETWEEN 1 & 9)
bool col_ok(int matrix[][SIDE], int col)
{
int digit[9] = {0};
for(int row = 0; row < SIDE; row++)
digit[ matrix[row][col] - 1 ] = 1;
for (int i = 0; i < 9; i++)
{
if(digit[i] == 0)
return false;
}
return true;
}
|