标签:style blog http io ar color os sp for
2 3 4 1 0 0 0 0 0 1 1 1 1 1 0 5 5 1 1 1 1 0 0 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 1 1 1
2 3
#include<stdio.h>
#include<string.h>
int s[105][105];
int a[4]={0,0,1,-1};//向四个方向搜索。
int b[4]={1,-1,0,0};
void bfs(int x,int y)
{
int k,v,t;
s[x][y]=0;
for(k=0;k<4;k++)
{
v=x+a[k];
t=y+b[k];
if(s[v][t])
{
bfs(v,t);
}
}
}
int main()
{
int test,n,m,i,j,ans;
scanf("%d",&test);
while(test--)
{
scanf("%d%d",&m,&n);
memset(s,0,sizeof(s));
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&s[i][j]);
}
for(i=1,ans=0;i<=m;i++)
{
for(j=1;j<=n;j++)
if(s[i][j])
{
bfs(i,j);
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
标签:style blog http io ar color os sp for
原文地址:http://blog.csdn.net/hdd871532887/article/details/41452065