fjnu 1337 最大黑区域

Description

二值图像是由黑白两种像素组成的矩形点阵,图像识别的一个操作是求出图像中最大黑区域的面积。请设计一个程序完成二值图像的这个操作。黑区域由黑像素组成,一个黑区域中的每个像素至少与该区域中的另一个像素相邻,规定一个像素仅与其上、下、左、右的像素相邻。两个不同的黑区域没有相邻的像素。一个黑区域的面积是其所包含的像素的个数。

Input

输入由多个测试例组成。每个测试例的第一行含两个整数n和m , (1<=n, m<=100), 分别表示二值图像的行数与列数,后面紧跟着n行,每行含m个整数0或1,其中第i行表示图像的第i行的m个像素,0表示白像素,1表示黑像素。同一行的相邻两个整数之间用一个空格隔开,两个测试例之间用一个空行隔开,最后一个测试例之后隔一个空行,再接的一行含有两个整数0,标志输入的结束。

Output

每个测试例对应一行输出,含一个整数,表示相应的图像中最大黑区域的面积。

Sample Input

5 6
0 1 1 0 0 1
1 1 0 1 0 1
0 1 0 0 1 0
0 0 0 1 1 1
1 0 1 1 1 0
0 0

 

Sample Output

7

 

KEY:搜索题,用队列做广度搜索

 

Source:

#include
< iostream >
using   namespace  std;

int  n,m;
int  M[ 150 ][ 150 ];
int  f[ 150 ][ 150 ];

struct  node
{
    
int x;
    
int y;
}
;

class  Queue
{
    
int front;
    
int rear;
    node elem[
20000];
public:
    Queue()
    
{
        front
=rear=1;
    }

    
void Init();
    
void EnQueue(int x,int y);
    node DeQueue();
    
int Empty();
}
;

void  Queue::Init()
{
    front
=rear=1;
}


void  Queue::EnQueue( int  x, int  y)
{
    node t;
    t.x
=x;
    t.y
=y;
    elem[rear
++]=t;
}


node Queue::DeQueue()
{
    
return elem[front++];
}


int  Queue::Empty()
{
    
if(front==rear) return 1;
    
else return 0;
}


int  count( int  a, int  b)
{
    
int N=0;
    Queue Q;
    Q.EnQueue(a,b);
    f[a][b]
=1;
    N
++;
    
while(!Q.Empty())
    
{
        node t;
        t
=Q.DeQueue();
        
int x,y;
        x
=t.x;
        y
=t.y;
        
if(y-1>0&&f[x][y-1]==0&&M[x][y-1]==1)
        
{
            Q.EnQueue(x,y
-1);
            f[x][y
-1]=1;
            N
++;
        }


        
if(x>0&&f[x-1][y]==0&&M[x-1][y]==1)
        
{
            Q.EnQueue(x
-1,y);
            f[x
-1][y]=1;
            N
++;
        }


        
if(y+1<=n&&f[x][y+1]==0&&M[x][y+1]==1)
        
{
            Q.EnQueue(x,y
+1);
            f[x][y
+1]=1;
            N
++;
        }


        
if(x+1>0&&f[x+1][y]==0&&M[x+1][y]==1)
        
{
            Q.EnQueue(x
+1,y);
            f[x
+1][y]=1;
            N
++;
        }

    }

    
return N;
}


void  f_init()
{
    
int i,j;
    
for(i=1;i<=m;i++)
        
for(j=1;j<=n;j++)
            f[i][j]
=0;
}


void  M_init()
{
    
int i,j;
    
for(i=1;i<=m;i++)
        
for(j=1;j<=n;j++)
            M[i][j]
=0;
}


int  main()
{
//   freopen("fjnu_1337.in","r",stdin);
    cin>>m>>n;
    M_init();
    f_init();
while(m!=0||n!=0)
{    
    
int max=0;
    
int i,j;
    
for(i=1;i<=m;i++)
        
for(j=1;j<=n;j++)
            cin
>>M[i][j];    
    
for(i=1;i<=m;i++)
        
for(j=1;j<=n;j++)
        
{
            
if(M[i][j]==1)
            
{
                
int t=0;
                t
=count(i,j);
                
if(t>max) max=t;
            }

        }

    cout
<<max<<endl;
    cin
>>m>>n;
    M_init();
    f_init();
}

return 0;
}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值