zoukankan      html  css  js  c++  java
  • codevs 1569 最佳绿草

    时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 黄金 Gold
     
    题目描述 Description


        贝茜正计划着这一天如何美美地咀嚼春天的绿草,远望着农民约翰钟爱的并被分
    割为R (1 <= R <= 100) 行和 C (1 <= C <= 100) 列的草场。她想去数一数草场
    有多少个草丛。

        每个草丛在地图上用'#'来表示,或者两个'#'连在一起(但不是在一个对角线),
    给出草场地图,请告诉贝茜草场上一共有多少个草丛。

        例如,下面有一张草场地图 R=5, C=6:

        .#....
        ..#...
        ..#..#
        ...##.
        .#....

        这个草场一共有5个草丛。(1,2);(2,3)+(3+3);(3,6);(4,4)+(4,5);(5,2)

    输入描述 Input Description


        * 第 1 行: 2个用空格隔开的整数 R , C

        * 第 2 至 R+1 行: 草场地图信息

    输出描述 Output Description

        * 草场上草丛的总个数。

    样例输入 Sample Input

    5 6
    .#....
    ..#...
    ..#..#
    ...##.
    .#....

    样例输出 Sample Output

    5

    #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;
    char 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++)
            {
                char c;
                cin>>c;
                if(c=='#')a[i][j]=1;
                else a[i][j]=0;
            }
        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;
    }
  • 相关阅读:
    mysql主从延迟高的原因
    OpenStack云平台网络模式及其工作机制
    maps.reg
    dnion的remap.conf文件
    linux的tar命令
    traffic server文件目录
    records.config文件参数解释
    VNC配置
    KVM详情
    cache.config文件配置模板
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7096436.html
Copyright © 2011-2022 走看看