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

codevs 1569 最佳绿草

时间:2017-06-29 23:48:08      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:using   clu   wrap   turn   pre   printf   style   计划   地图   

时间限制: 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;
}

 

codevs 1569 最佳绿草

标签:using   clu   wrap   turn   pre   printf   style   计划   地图   

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

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