typedef struct mat
{
int n, r, c;
}matParm;
matParm sub_mat; // Global Variable
/* Call from main function */
if( row > col)
for (i = 0; i < row - col; i++)
find_sqr_mat (matrix, i, 0, col);
else if (col > row)
for (i = 0; i < col - row ; i++)
find_sqr_mat (matrix, 0, i, row);
else
find_sqr_mat (matrix, 0, 0, col);
/* Print the out put matrix */
printf("\n Largest Sub Square Matrix\n");
printf("Row = %d Col = %d N = %d\n",sub_mat.r, sub_mat.c, sub_mat.n);
for (i = sub_mat.r; i < sub_mat.r + sub_mat.n; i++)
{
printf("\n\n");
for (j = sub_mat.c; j < sub_mat.c + sub_mat.n; j++)
printf("%d ", matrix[i][j]);
}
/* Function to find the largest squar matrix */
matParm * find_sqr_mat(int **M, int row, int col, int N)
{
int n = 0;
matParm * tempparm;
matParm *tempparm1, *tempparm2, * tempparm3, * tempparm4;
tempparm = (matParm *)malloc(sizeof(matParm));
if ( N==1 )
{
if(M[row][col] == 1)
{
tempparm->n = N;
tempparm->r = row;
tempparm->c = col;
}
else
{
tempparm->n = 0;
tempparm->r = 0;
tempparm->c = 0;
}
}
else
{
tempparm1 = find_sqr_mat(M, row, col, N-1);
tempparm2 = find_sqr_mat(M, row+1, col, N-1);
tempparm3 = find_sqr_mat(M, row, col+1, N-1);
tempparm4 = find_sqr_mat(M, row+1, col+1, N-1);
if((tempparm1 && tempparm2 && tempparm3 && tempparm4) &&
(tempparm1->n == tempparm2->n) &&
(tempparm2->n == tempparm3->n) &&
(tempparm3->n == tempparm4->n) &&
(tempparm4->n == N-1))
{
tempparm->n = N;
tempparm->r = row;
tempparm->c = col;
if (sub_mat.n < N)
{
sub_mat.n = N;
sub_mat.r = row;
sub_mat.c = col;
}
free(tempparm1);
free(tempparm2);
free(tempparm3);
free(tempparm4);
return tempparm;
}
else
{
return NULL;
}
free(tempparm1);
free(tempparm2);
free(tempparm3);
free(tempparm4);
}
return tempparm;
}
Hi,
I have tested it for some examples.
Please let me know if any test case is failed.