Largest square formed in a matrix
PROBLEM :
Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is n and m,n is the number of rows and m is the number of columns.
The second line of each test case contains array C[n][m] in row major form.
Output:
Print maximum size square sub-matrix.
Constraints:
1 = T = 100
1 = n,m = 50
0 = C[n][m] = 1
Example:
Input:
3
5 6
0 1 0 1 0 1 1 0 1 0 1 0 0 1 1 1 1 0 0 0 1 1 1 0 1 1 1 1 1 1
2 2
1 1 1 1
2 2
0 0 0 0
Output:
3
2
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define R 50
#define C 50
int Largest_square(int mat[R][C],int ,int ) ;
int min(int ,int ,int ) ;
int main()
{
int t,r,c,ans,i,j,mat[R][C] ;
cin>>t ;
while(t--)
{
cin>>r>>c ;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>mat[i][j] ;
ans=Largest_square(mat,r,c) ;
cout<<ans<<endl ;
}
return 0;
}
int Largest_square(int mat[R][C],int r,int c)
{
int d[r+1][c+1] ;
int i,j,mx ;
for(i=0;i<=c;i++)
d[0][i]=0 ;
for(j=0;j<=r;j++)
d[j][0]=0 ;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(mat[i-1][j-1]==0)
d[i][j]=0 ;
else
d[i][j]=1+min(d[i-1][j-1],d[i-1][j],d[i][j-1]) ;
}
}
mx=d[0][0] ;
for(i=0;i<=r;i++)
{
for(j=0;j<=c;j++)
{
if(mx<d[i][j])
mx=d[i][j] ;
}
}
return mx ;
}
int min(int a,int b,int c)
{
int temp ;
temp=a<b?a:b ;
return c<temp?c:temp ;
}
---------------------------------------------------------------------------------
Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is n and m,n is the number of rows and m is the number of columns.
The second line of each test case contains array C[n][m] in row major form.
Output:
Print maximum size square sub-matrix.
Constraints:
1 = T = 100
1 = n,m = 50
0 = C[n][m] = 1
Example:
Input:
3
5 6
0 1 0 1 0 1 1 0 1 0 1 0 0 1 1 1 1 0 0 0 1 1 1 0 1 1 1 1 1 1
2 2
1 1 1 1
2 2
0 0 0 0
Output:
3
2
0
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define R 50
#define C 50
int Largest_square(int mat[R][C],int ,int ) ;
int min(int ,int ,int ) ;
int main()
{
int t,r,c,ans,i,j,mat[R][C] ;
cin>>t ;
while(t--)
{
cin>>r>>c ;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>mat[i][j] ;
ans=Largest_square(mat,r,c) ;
cout<<ans<<endl ;
}
return 0;
}
int Largest_square(int mat[R][C],int r,int c)
{
int d[r+1][c+1] ;
int i,j,mx ;
for(i=0;i<=c;i++)
d[0][i]=0 ;
for(j=0;j<=r;j++)
d[j][0]=0 ;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(mat[i-1][j-1]==0)
d[i][j]=0 ;
else
d[i][j]=1+min(d[i-1][j-1],d[i-1][j],d[i][j-1]) ;
}
}
mx=d[0][0] ;
for(i=0;i<=r;i++)
{
for(j=0;j<=c;j++)
{
if(mx<d[i][j])
mx=d[i][j] ;
}
}
return mx ;
}
int min(int a,int b,int c)
{
int temp ;
temp=a<b?a:b ;
return c<temp?c:temp ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment