22. Wide Search: Enclosed area

Source: Internet
Author: User

. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.