码迷,mamicode.com
首页 > 其他好文 > 详细

P1451 求细胞数量

时间:2018-07-22 00:10:24      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:--   联通   main   mes   har   str   tar   names   https   

https://www.luogu.org/problemnew/show/P1451<----这里是原题链接

这道题的题意大致是求一个矩阵中非0联通块的个数,那么我们可以dfs每一个块

对于每一个块,ans++后把所有块中的数字归零(吃掉)

所以代码是这样的

 1 #include<iostream>
 2 #include<queue>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn = 105;
 6 int dx[8]={-1,0,1,0};
 7 int dy[8]={0,1,0,-1};
 8 bool a[maxn][maxn];
 9 char c;
10 int n,m,ans=0;
11 void dfs(int x,int y)
12 {
13     for(int i=0;i<4;i++)
14     {
15         int xx=x+dx[i];
16         int yy=y+dy[i];
17         if(a[xx][yy]==1)
18         {
19             a[xx][yy]=0;
20             dfs(xx,yy);
21         }
22     }
23 }
24 int main()
25 {
26     cin>>n>>m;
27     for(int i=1;i<=n;i++)
28     {
29         for(int j=1;j<=m;j++)
30         {
31             cin>>c;
32             if(c==0)a[i][j]=0;
33             else a[i][j]=1;
34         }
35     }
36     for(int i=1;i<=n;i++)
37     {
38         for(int j=1;j<=m;j++)
39         {
40             if(a[i][j]==1)
41             {
42                 ans++;
43                 dfs(i,j);
44             }
45         }
46     }
47     cout<<ans;
48 }

那么这样子就解决啦!!!

P1451 求细胞数量

标签:--   联通   main   mes   har   str   tar   names   https   

原文地址:https://www.cnblogs.com/woshishabiye/p/9348278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!