相聚

链接:https://ac.nowcoder.com/acm/contest/558/J
来源:牛客网
 

小猫在研究网格图。

小猫在研究联通性。

给定一张N×M的网格图,只含字符0和1,问1形成的联通块有多少个。

两个1是联通的,当且仅当其中一个位于另一个的上、下、左、右四个方向之一。

输入描述:

第一行一个正整数T,表示数据组数。

每组数据的第一行两个正整数N,M,表示矩阵的长和宽。

接下来N行,每行M个字符0或1。

输出描述:

T行,每行一个正整数,表示每组数据的答案。

示例1

输入

复制

2
3 5
10101
01110
10101
3 3
111
010
111

输出

复制

5
1

备注:

1≤T,N,M≤50

思路:和油田一样bfs

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char a[1009][1009];int n,m;
int book[1009][1009];
struct node{
	int x,y,step;
	node(){
	}
	node(int xx,int yy,int sstep):x(xx),y(yy),step(sstep){
	} 
};
queue<node>A; 
const int rr[4][2] = {1,0,0,1,-1,0,0,-1};
void bfs(int x,int y,int step)
{
	A.push(node(x,y,step));
	book[x][y] = 1;
	while(!A.empty())
	{
		node u =A.front();
		A.pop();
		for(int i = 0;i < 4;++i)
		{
			int xx = u.x+rr[i][0];
			int yy = u.y+rr[i][1];
			int ss = u.step=1;
			if(xx<1 || xx >n || yy < 1 || yy > m || book[xx][yy] == 1 || a[xx][yy] == '0')
			{
				continue;
			} 
			book[xx][yy] = 1;
			A.push(node(xx,yy,ss));
		} 	
	}
	
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(book,0,sizeof(book));memset(a,0,sizeof(a));
		string h;
		scanf("%d%d",&n,&m);
		for(int i = 1 ;i <= n;++i)
		{
			cin>>h;
			for(int j = 1;j <= m;++j)
			{
				a[i][j] = h[j-1];
			}
		}int ans =0;
		for(int i = 1 ;i <= n;++i)
		{
			for(int j = 1;j <= m;++j)
			{
				if(a[i][j] == '1' && book[i][j] == 0)
				{
					bfs(i,j,0);ans++;
				} 
			}
		}
		printf("%d\n",ans);
	}
	return 0;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值