There is 2D array of 1’s and 0’s having the maximum number of rows and columns not more than 100 . Write a program to find the size of the largest area.
Note: The largest area is the set of elements containing 1’s that are adjacent to each other in any of the eight directions and the largest area is linear.
Input
In the first line n is given and in the following lines , each line represents a row of 2D array.
Output
Print each answer on a new line , the answer is the size of the largest area.
Eg:
Input:7
1
|
0
|
0
|
0
|
1
|
0
|
0
|
0
|
1
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
0
|
1
|
0
|
0
|
0
|
0
|
1
|
1
|
0
|
1
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
0
|
1
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
Output: 5
Soln:
#include<stdio.h>
#include<conio.h>
int
n;
int
AnswerN;
int
tempMatrix [n][n];
int
matrix[n][n];
int
countforindex(int i,int j, int n);
int
main (void)
{
int
k,l;
int
tempmatrixconsecutiveone = 0;
int i,j;
scanf(“%d”,&n);
for (i=0;i<n;i++)
for(j=0;j<n;j++)
scanf(“%d”,&matrix[i][j]);
tempmatrixconsecutiveone=0;
AnswerN=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(matrix[i][j]==1)
{
for(k=0;k<n;k++)
for(l=0;l<n;l++)
{
tempMatrix [k][l]= matrix[k][l];
}
tempMatrix[i][j]=0;
tempmatrixconsecutiveone=1;
tempmatrixconsecutiveone = tempmatrixconsecutiveone + countforindex(i,j,n);
AnswerN= AnswerN> tempmatrixconsecutiveone? AnswerN :
tempmatrixconsecutiveone
;
}
}
printf(“%d”, AnswerN);
return
0;
}
int
countforindex(int i,int j, int n)
{
{
int resultZ=0;
int a_ic,b_ic,c_ic,d_ic, e_ic,f_ic,g_ic,h_ic;
int a_ic,b_ic,c_ic,d_ic, e_ic,f_ic,g_ic,h_ic;
int a_jc,b_jc,c_jc,d_jc ,e_jc,f_jc,g_jc,h_jc;
a_ic=i-1;
a_jc=j-1;
b_ic=i-1;
b_jc=j;
c_ic=i-1;
c_jc=j+1;
d_ic=i;
d_jc=j-1;
e_ic=i;
e_jc=j+1;
f_ic=i+1;
f_jc=j-1;
g_ic=i+1;
g_jc=j;
h_ic=i+1;
h_jc=j+1;
a_jc=j-1;
b_ic=i-1;
b_jc=j;
c_ic=i-1;
c_jc=j+1;
d_ic=i;
d_jc=j-1;
e_ic=i;
e_jc=j+1;
f_ic=i+1;
f_jc=j-1;
g_ic=i+1;
g_jc=j;
h_ic=i+1;
h_jc=j+1;
if(a_ic>-1 && a_ic<n && a_jc>-1 && a_jc<n)
if(tempMatrix[a_ic][a_ jc]
== 1)
{
tempMatrix[a_ic][a_jc]=0;
resultZ = resultZ +1+ countforindex(a_ic,a_jc,n);
{
tempMatrix[a_ic][a_jc]=0;
}
if(b_ic>-1 && b_ic<n && b_jc>-1 && b_jc<n)
if(tempMatrix[b_ic][ b_jc]
== 1) {
tempMatrix[b_ic][b_jc]=0;
resultZ = resultZ +1+ countforindex(b_ic,b_jc,n);
tempMatrix[b_ic][b_jc]=0;
resultZ = resultZ +1+ countforindex(b_ic,b_jc,n);
}
if(c_ic>-1 && c_ic<n && c_jc>-1 && c_jc<n)
if(tempMatrix[c_ic][c_jc] == 1)
{
tempMatrix[c_ic][c_jc]=0;
tempMatrix[c_ic][c_jc]=0;
resultZ = resultZ+1+ countforindex(c_ic,c_jc,n);
}
if(d_ic>-1 && d_ic<n && d_jc>-1 && d_jc<n)
if(tempMatrix[d_ic][d_jc] == 1)
{
tempMatrix[d_ic][d_jc]=0;
resultZ = resultZ+1+ countforindex(d_ic,d_jc,n);
}
if(e_ic>-1 && e_ic<n && e_jc>-1 && e_jc<n)
if(tempMatrix[e_ic][e_jc] == 1)
{
tempMatrix[e_ic][e_jc]=0;
tempMatrix[e_ic][e_jc]=0;
resultZ = resultZ+1+ countforindex(e_ic,e_jc,n);
}
if(f_ic>-1 && f_ic<n && f_jc>-1 && f_jc<n)
if(tempMatrix[f_ic][f_jc] == 1)
{
tempMatrix[f_ic][f_jc]=0;
resultZ = resultZ+1+ countforindex(f_ic,f_jc,n);
}
if(g_ic>-1 && g_ic<n && g_jc>-1 && g_jc<n)
if(tempMatrix[g_ic][g_ jc]
== 1)
{
tempMatrix[g_ic][g_jc]=0;
resultZ = resultZ+1+ countforindex(g_ic,g_jc,n);
}
if(h_ic>-1 && h_ic<n && h_jc>-1 && h_jc<n)
if(h_ic>-1 && h_ic<n && h_jc>-1 && h_jc<n)
if(tempMatrix[h_ic][h_jc] == 1)
{
tempMatrix[h_ic][h_ jc]=0;
resultZ = resultZ+1+ countforindex(h_ic,h_jc,n);
}
return resultZ;
}