面积

面积

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 4

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

编程计算由“*”号围成的下列图形的面积。面积计算方法是统计*号所围成的闭合曲线中水平线和垂直线交点的数目。如下所示,在10*10的二维数组中,有“*”围住了15个点,因此面积为15。
0 0 0 0 0 0 0 0 0 0
0 0 0 0 * * * 0 0 0
0 0 0 0 * 0 0 * 0 0
0 0 0 0 0 * 0 0 * 0
0 0 * 0 0 0 * 0 * 0
0 * 0 * 0 * 0 0 * 0
0 * 0 0 * * 0 * * 0
0 0 * 0 0 0 0 * 0 0
0 0 0 * * * * * 0 0
0 0 0 0 0 0 0 0 0 0

Input

输入有多组数据,每组数据是10*10的二维数组。

Output

对于每组数据输出面积。

Sample Input

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

15

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int p[20][20];
int main ()
{ //freopen("a.txt","r",stdin);
    int k;
    while(scanf("%d",&k)!=EOF)
    {  int cnt=0;
        memset(p,0,sizeof(p));
        p[1][1]=k;
        for(int i=1;i<=10;i++)
        {
            if(i==1)
                for(int j=2;j<=10;j++)
                scanf("%d",&p[i][j]);
            else
            for(int j=1;j<=10;j++)
                scanf("%d",&p[i][j]);
        }
        for(int i=1;i<=10;i++)
            {
                if(p[i][1]==0)  p[i][1]=-1;
                if(p[i][10]==0)     p[i][10]=-1;
            }
        for(int j=1;j<=10;j++)
        {
            if(p[1][j]==0) p[1][j]=-1;
             if(p[10][j]==0)p[10][j]=-1;
        }

        for(int i=1;i<=10;i++)
            for(int j=1;j<=10;j++)
            if(p[i][j]==0&&(p[i-1][j]==-1||p[i+1][j]==-1||p[i][j-1]==-1||p[i][j+1]==-1))
            p[i][j]=-1;
        for(int i=10;i>=1;i--)
            for(int j=10;j>=1;j--)
             if(p[i][j]==0&&(p[i-1][j]==-1||p[i+1][j]==-1||p[i][j-1]==-1||p[i][j+1]==-1))
            p[i][j]=-1;
        for(int i=1;i<=10;i++)
            for(int j=1;j<=10;j++)
            if(p[i][j]==0)
            cnt++;
            printf("%d\n",cnt);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u010760034/article/details/17288321
今日推荐