#define SIZE 4
void restoreArray(bool A[], int size)
{
int i = 0;
for(i = 0; i < size; ++i)
{
A[i] = false;
}
}
bool CheckLatinSquare(int A[][SIZE],int size)
{
bool a[SIZE];
int i = 0, j = 0;
restoreArray(a,size);
for(i = 0; i < size; ++i)
{
for (j = 0; j < size; ++j)
if (a[A[i][j]] < 0 || a[A[j][i]] >= size)
return false;
else if (a[A[i][j]] == true)
return false;
else
a[A[i][j]] = true;
restoreArray(a,size);
}
for(i = 0; i < size; ++i)
{
for (j = 0; j < size; ++j)
if (a[A[j][i]] < 0 || a[A[j][i]] >= size)
return false;
else if (a[A[j][i]] == true)
return false;
else
a[A[j][i]] = true;
restoreArray(a,size);
}
return true;
}
void main()
{
int a[SIZE][SIZE] = {{1,3,0,2},
{3,2,1,0},
{2,0,3,1},
{0,1,2,3},};
int b[SIZE][SIZE] = {{1,3,0,2},
{3,2,1,0},
{3,0,2,1},
{0,1,2,3},};
if (CheckLatinSquare(a,SIZE) == true)
printf("a is a latin square.\n");
else
printf("a is NOT a latin square.\n");
if (CheckLatinSquare(b,SIZE) == true)
printf("b is a latin square.\n");
else
printf("b is NOT a latin square.\n");
} |