洛谷.P1451 求细胞数量

洛谷.P1451 求细胞数量

题目描述
一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右若还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。(1<=m,n<=100)?
输入输出格式
输入格式:

输入:整数m,n(m行,n列)
矩阵

输出格式:

输出:细胞的个数

输入输出样例
输入样例#1:
4 10
0234500067
1034560500
2045600671
0000000089

输出样例#1:
4

#include <cstdio>
#include <iostream>
#include<queue>
#include<cstring>
using namespace std;
int m, n, i, j;
int dir[4][2] = { -1, 0, 1, 0, 0, 1, 0, -1 };
int vis[110][110];
char s[110][110];
int ans;
struct node{
	int x, y;
};
bool checkedge(int x, int y){
	if (x<0 || x>=m || y<0 || y>=n || vis[x][y] == 1 || s[x][y]=='0')
		return 1;
	return 0;
}
void bfs(int x, int y){
    ans++;
	node now, next;
	queue<node>q;
	now.x = x;
	now.y = y;
	vis[x][y] = 1;
	q.push(now);
	while (!q.empty()){
		int nx, ny;
		now = q.front();
		q.pop();
		for (int k = 0; k < 4; k++){
			nx = now.x + dir[k][0];
			ny = now.y + dir[k][1];
			if (checkedge(nx, ny))
				continue;
			next.x = nx;
			next.y = ny;
			q.push(next);
			vis[nx][ny] = 1;
		}
	}
	return;
}
int main(){
	cin >> m >> n;
	for (i = 0; i < m; i++)
		cin >> s[i];
	for (i = 0; i <m; i++){
		for (j = 0; j < n; j++){
			if (s[i][j] != '0' && vis[i][j] == 0)
				bfs(i, j);
		}
	}
	cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值