A:细胞

Problem A: 细胞

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 129  Solved: 91
[Submit][Status][Web Board]

Description

一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数。

Input

整数m,n(m行,n列)1<=N,M<=100

Output

细胞的个数

Sample Input

4 10
0234500067
1034560500
2045600671
0000000089

Sample Output

4

HINT

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,1,-1};
 4 int num,m,n;
 5 bool b[51][81];
 6 char a[51][81];
 7 int h[4000][3];
 8 void bfs(int p,int q)
 9 {
10     int head,tail,x,y;
11     num++;
12     b[p][q]=false;
13     head=1;
14     tail=1;
15     h[1][1]=p;
16     h[1][2]=q;
17     while(head<=tail)
18     {
19         for (int i=1;i<=4;i++)
20         {
21             x=h[head][1]+dx[i];
22             y=h[head][2]+dy[i];
23             if ((x>=1) && (y>=1) && (x<=m) && (y<=n) && (b[x][y]==true))
24             {
25                 tail++;
26                 h[tail][1]=x;
27                 h[tail][2]=y;
28                 b[x][y]=0;
29             }    
30         }
31         head++;
32     }
33 }
34 int main()
35 {
36     cin>>m>>n;
37     for(int i=1;i<=m;i++)
38     for(int j=1;j<=n;j++)
39     {
40         cin>>a[i][j];
41         if (a[i][j]!='0')
42         b[i][j]=true;
43     }
44     for (int i=1;i<=m;i++)
45     for (int j=1;j<=n;j++)
46     {
47         if (b[i][j]==true)
48         bfs(i,j);
49     }
50     cout<<num<<endl;
51     return 0;
52 }
cell

 

转载于:https://www.cnblogs.com/LHR-HY/p/6785747.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值