单点时限: 2.0 sec
内存限制: 256 MB
二值图像是由黑白两种像素组成的矩形点阵,图像识别的一个操作是求出图像中次大 (第二大) 黑区域的面积。请设计一个程序完成二值图像的这个操作。黑区域由若干黑像素组成,一个黑区域中的每个黑像素至少与该区域中的另一个黑像素相邻,规定一个像素仅与其上、下、左、右的像素相邻。两个不同的黑区域没有相邻的像素。一个黑区域的面积是其所包含的黑像素的个数。
输入格式
第1 行:一个整数 T (1≤T≤10) 为问题数。
接下来共 T 组测试数据,每组测试数据的第一行含两个整数 n,m (1≤n,m≤100),分别表示二值图像的行数与列数,后面紧跟着 n 行,每行含 m 个整数 0 或 1,其中第 i 行表示图像的第 i 行的 m 个像素,0 表示白像素,1 表示黑像素。同一行的相邻两个整数之间用一个空格隔开。
输出格式
对于每个问题,输出一行问题的编号(0 开始编号,格式:case #0: 等)。
然后对应每组测试数据,在一行中输出一个整数,表示相应的图像中次大(第二大)黑区域的面积。如果第二大面积的黑区域不存在时,则输出 0。
样例
input
3
2 3
1 1 0
0 1 1
4 5
0 1 1 0 1
0 0 0 1 0
0 1 1 1 0
0 1 0 0 0
5 6
1 1 0 0 1 1
1 1 0 1 1 0
0 0 1 0 0 1
1 1 0 1 0 1
0 1 0 1 1 0
output
case #0:
0
case #1:
2
case #2:
3
提示
第一组测试数据:
1 1 0
0 1 1
在图像中只能找到 1 个黑区域,面积为 4,因此次大(第二大)黑区域的面积为 0。
第三组测试数据:
1 1 0 0 1 1
1 1 0 1 1 0
0 0 1 0 0 1
1 1 0 1 0 1
0 1 0 1 1 0
在图像中能找到 6 个黑区域,它们的面积分别为 4,4,3,3,2,1,次大(第二大)黑区域的面积为 3。
/*
思路:bfs求连通分量
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<set>
#define P pair<int,int>
using namespace std;
int n,m;
bool visit[100][100]= {false};
int x[4]= {0,0,1,-1};
int y[4]= {1,-1,0,0};
int d[100][100];
bool text(int a,int b) {
if(a<0||b<0||a>=n||b>=m||visit[a][b]||d[a][b]==0)
return false;
return true;
}
int bfs(int a,int b) {
queue<P>q;
P p(a,b);
visit[a][b]=true;
q.push(p);
int ans=1;
while(!q.empty()) {
P f=q.front();
q.pop();
for(int i = 0; i < 4; i++) {
a =f.first+x[i];
b =f.second+y[i];
if(text(a,b)) {
P m(a,b);
q.push(m);
ans++;
visit[a][b]=true;
}
}
}
return ans;
}
int main() {
int t;
cin>>t;
for(int z = 0; z < t; z++) {
cin>>n>>m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++)
cin>>d[i][j];
}
memset(visit,0,sizeof(visit));
set<int>s;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(visit[i][j]==false&&d[i][j]==1) {
s.insert(bfs(i,j));
}
}
}
printf("case #%d:\n",z);
if(s.size()<2)
cout<<0<<endl;
else {
//for(set<int>::iterator it = s.begin(); it!=s.end(); it++) {
set<int>::iterator it=s.end();
it--;
it--;
cout<<*it<<endl;
}
}
return 0;
}