Counting Sheep

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 38   Accepted Submission(s) : 27

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'd gone to bed. As always when my grandmother suggests things, I decided to try it out.
The only problem was, there were no sheep around to be counted when I went to bed.






Creative as I am, that wasn't going to stop me. I sat down and wrote a computer program that made a grid of characters, where # represents a sheep, while . is grass (or whatever you like, just not sheep). To make the counting a little more interesting, I also
decided I wanted to count flocks of sheep instead of single sheep. Two sheep are in the same flock if they share a common side (up, down, right or left). Also, if sheep A is in the same flock as sheep B, and sheep B is in the same flock as sheep C, then sheeps
A and C are in the same flock.





Now, I've got a new problem. Though counting these sheep actually helps me fall asleep, I find that it is extremely boring. To solve this, I've decided I need another computer program that does the counting for me. Then I'll be able to just start both these
programs before I go to bed, and I'll sleep tight until the morning without any disturbances. I need you to write this program for me.

Input

The first line of input contains a single number T, the number of test cases to follow.



Each test case begins with a line containing two numbers, H and W, the height and width of the sheep grid. Then follows H lines, each containing W characters (either # or .), describing that part of the grid.

Output

For each test case, output a line containing a single number, the amount of sheep flock son that grid according to the rules stated in the problem description.



Notes and Constraints

0 < T <= 100

0 < H,W <= 100

Sample Input

2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###

Sample Output

6
3

Source

IDI Open 2009



———————————————————————————————————————————————————
求#的连通块的数量,直接dfs


#include<iostream>
#include<cmath>
using namespace std;
char map[105][105];
int m, n, t;
int dir[4][2] = { { -1, 0 },{ 0, -1 }, { 0, 1 },{ 1, 0 } }; void dfs(int si, int sj)
{
    if (si <= 0 || sj <= 0 || si > m || sj > n)
        return;
    for (int i = 0; i < 4; i++)
    {
        if (map[si + dir[i][0]][sj + dir[i][1]] != '.')
        {
            map[si + dir[i][0]][sj + dir[i][1]] = '.';
            dfs(si + dir[i][0], sj + dir[i][1]);
        }
    }
    return;
}
int main()
{
    int o;
    while(~scanf("%d",&o))
    {
        while(o--)
        {
            cin >> m >> n;
            {
                for (int i = 1; i <= m;i++)
                    for (int j = 1; j <= n; j++)
                        cin >> map[i][j];
                t = 0;
                for (int i = 1; i <= m; i++)
                    for (int j = 1; j <= n; j++)
                    {
                        if (map[i][j] == '#')
                        {
                            map[i][j] = '.';
                            t++;
                            dfs(i, j);
                        }
                    }
                    printf("%d\n", t);
            }
        }
    }
        return 0;     }


Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏的更多相关文章

  1. Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏

    C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...

  2. HDU1426 Sudoku Killer(DFS暴力) 2016-07-24 14:56 65人阅读 评论(0) 收藏

    Sudoku Killer Problem Description 自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视. 据说,在2008北京奥运会上,会 ...

  3. Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏

    哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  4. Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏

    以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...

  5. HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏

    Coprime Sequence                                                        Time Limit: 2000/1000 MS (Ja ...

  6. MS SQL数据批量备份还原(适用于MS SQL 2005+) 分类: SQL Server 数据库 2015-03-10 14:32 103人阅读 评论(0) 收藏

    我们知道通过Sql代理,可以实现数据库的定时备份功能:当数据库里的数据库很多时,备份一个数据库需要建立对应的定时作业,相对来说比较麻烦: 还好,微软自带的osql工具,比较实用,通过在命令行里里输入命 ...

  7. iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  8. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  9. iOS中UITextField 使用全面解析 分类: ios技术 2015-04-10 14:37 153人阅读 评论(0) 收藏

    //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 13 ...

随机推荐

  1. How to Pronounce the word BECAUSE

    How to Pronounce the word BECAUSE Share Tweet Share Tagged With: BECAUSE Reduction Study the BECAUSE ...

  2. 转载 JS组件Bootstrap Select2使用方法详解

    JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...

  3. hibernate 解决并发问题

    hibernate 解决并发问题的策略有 1)设置hibernate事务隔离级别 2)hibernate中乐观锁的实现 ps:版本号是由hibernate自己维护的,我们自己只需要做以上二步即可实现乐 ...

  4. 201671010140. 2016-2017-2 《Java程序设计》java学习第四周

    java学习第四周体会         本周,与前几周不同的是,老师没有进行课堂测试,而是上了一节课,回顾与总结了之前三周所学的知识,也是因为这节课,我注意到了之前学习中忽略的一些细节,和之前学习方法 ...

  5. JPA报错, java.lang.NullPointerException

    java.lang.NullPointerException 我觉得这应该是一个很常见的错误, 数据库没取到数据嘛, 很正常, JPA没取到数据就是会抛出空指针异常, 但是就是这么简单的一个错误也让我 ...

  6. cdoj203-Islands 【并查集】

    http://acm.uestc.edu.cn/#/problem/show/203 Islands Time Limit: 30000/10000MS (Java/Others)     Memor ...

  7. SqlServer把日期转换成不同格式的字符串的函数大全

    SQL语句                                        结果SELECT CONVERT(varchar(100), GETDATE(), 0)-- 05 03 20 ...

  8. 最高频的K个单词 · Top K Frequent Words

    [抄题]: 给一个单词列表,求出这个列表中出现频次最高的K个单词. [思维问题]: 以为已经放进pq里就不能改了.其实可以改,利用每次取出的都是顶上的最小值就行了.(性质) 不知道怎么处理k个之外的数 ...

  9. Linux SSH基于密钥交换的自动登陆原理简介及配置说明

    一.原理简介 SSH证书认证登录的基础是一对唯一匹配密钥: 私钥(private key)和公钥(public key).公钥用于对数据进行加密,而且只能用于加密.而私钥只能对使用所匹配的公钥,所加密 ...

  10. php单点登陆简单实现 (iframe方式)

    有四个网站分别为: www.a.com www.b.com www.c.com www.sso.com 需求是如果我们在sso登陆后,其他网站也会显示登陆中,不需要重复登陆,退出时,其他网站也会失效. ...