一个由“1”围成的图形,其面积的计算方法是统计“1”所围成的闭合曲线中水平线和垂直线交点的数目。如图所示,在10×10的二维数组中,由“1”围住15个点,因此面积为15。
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
输入
一个10×10的二维数组,里面的数为0和1
输出
#include <bits/stdc++.h>
using namespace std;
int a [11] [11] , b [11] [11] ;
int n , sum = 0 ;
void dfs ( int x , int y )
{
if ( x < 0 || x > n + 1 || y < 0 || y > n + 1 || a [x] [y] == 2 )
{
return ;
}
a [x] [y] = 1 ;
dfs ( x + 1 , y ) ;
dfs ( x - 1 , y ) ;
dfs ( x , y + 1 ) ;
dfs ( x , y - 1 ) ;
return ;
}
int main ( )
{
for ( int i = 1 ; i <= 10 ; i ++ )
for ( int j = 1 ; j <= 10 ; j ++ )
{
cin >> b [i] [j] ;
if ( ! b [i] [j] )
{
a [i] [j] = 0 ;
}
else
{
a [i] [j] = 2 ;
}
}
dfs ( 0 , 0 ) ;
cout << endl ;
for ( int i = 1 ; i <= 10 ; i ++ )
{
for ( int j = 1 ; j <= 10 ; j ++ )
{
if ( a [i] [j] )
{
cout << "X " ;
sum ++ ;
}
else
{
cout << a [i] [j] << ' ' ;
}
}
cout << endl ;
}
cout << sum << endl ;
return 0 ;
}