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

luogu P1451 求细胞数量

时间:2017-06-27 20:51:25      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:char   data-   tchar   color   bool   输入输出格式   pop   for   矩阵   

P1451 求细胞数量

题目描述

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

输入输出格式

输入格式:

 

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

矩阵

 

输出格式:

 

输出:细胞的个数

 

输入输出样例

输入样例#1:
4  10
0234500067
1034560500
2045600671
0000000089
输出样例#1:
4
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>

using namespace std;
const int N=101;
const int xd[]={0,-1,0,1};
const int yd[]={-1,0,1,0};

struct node{
    int x,y;
}now,top,nxt;
int a[N][N];
bool vis[N][N];
int answer;
int n,m;
queue<node>q;

inline int read()
{
    int x=0;char c=getchar();
    while(c<0||c>9)c=getchar();
    return x=c-0;
}

inline void bfs(int x,int y)
{
    
    answer++;
    now.x=x;
    now.y=y;
    q.push(now);
    vis[x][y]=1;
    
    while(!q.empty())
    {
        top=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=xd[i]+top.x;
            int yy=yd[i]+top.y;
            if(a[xx][yy]&&xx>0&&xx<=n&&yy>0&&yy<=m)
            {
                a[xx][yy]=0;
                nxt.x=xx;
                nxt.y=yy;
                q.push(nxt);
            }
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            a[i][j]=read();
    
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j])
                bfs(i,j);
                    
    printf("%d",answer);
    return 0;
}

 

luogu P1451 求细胞数量

标签:char   data-   tchar   color   bool   输入输出格式   pop   for   矩阵   

原文地址:http://www.cnblogs.com/lyqlyq/p/7086883.html

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