. Guangzhou Search: Enclosed area
Programming calculates the area of the following graph, enclosed by the "*" number. The area calculation method is the number of intersection points of the horizontal and vertical lines in the closed curve enclosed by the statistic * number. As shown in the two-dimensional array of 10*10, there is "*" enclosing 15 points, so the area is 15.
"Sample Input" area.in
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 1 0
0 1 0 1 0 1 0 0 1 0
0 1 0 0 1 1 0 1 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
"Sample Output" Area.out
15
Idea: Because the boundary must not be surrounded, the boundary of 0 are set to 2, start wide search, if a 0 up and down position there is a 2, then this 0 will not be surrounded, the assignment is 2, and finally count how many 0 can be
Code:
#include
using namespace Std;
#include
#define MAXN 101
int P[MAXN][MAXN];
int n;
int main ()
{
n=10;
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
{
scanf ("%d", &p[i][j]);
if (i==1| | i==n) &&p[i][j]==0) p[i][j]=2;
if (j==1| | j==n) &&p[i][j]==0) p[i][j]=2;
}
for (int i=2,i1=n-1;i
for (int j=2;j<=n-1;++j)
{
if (p[i][j]==0)
{
if (p[i+1][j]==2| | p[i-1][j]==2| | p[i][j+1]==2| | p[i][j-1]==2)
p[i][j]=2;
}
if (p[i1][j]==0)
{
if (p[i1+1][j]==2| | p[i1-1][j]==2| | p[i1][j+1]==2| | p[i1][j-1]==2)
p[i1][j]=2;
}
}
int sum=0;
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
{
if (p[i][j]==0)
sum++;
}
printf ("%d", sum);
return 0;
}
22. Wide Search: Enclosed area