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. oracle与DB2

    1.体系结构,DB2的实例和数据库分开的做法,我个人还是比较喜欢的,因为实例可以创建多个,数据库的恢复直接恢复到实例下就可以了,相对ORACLE简单多了. 2.管理工具,DB2的管理工具做得太简陋了, ...

  2. 一个团队和他们的调查表-----("调查表与调查结果分析"心得体会)

    注:这篇blog主要是描述六小灵童团队在从接到调查表任务到分析调查数据最后完成本次任务的过程,以及过程中的点滴和心德体会.---蔡何 1.制表历程 随着课程的推进,我们逐步进入了软件项目中比较重要的需 ...

  3. 重哈希 · rehashing

    [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: newindex = (hashTable[ ...

  4. K组翻转链表 · Reverse Nodes in k-Group

    [抄题]: 给你一个链表以及一个k,将这个链表从头指针开始每k个翻转一下.链表元素个数不是k的倍数,最后剩余的不用翻转. [思维问题]: [一句话思路]: // reverse head->n1 ...

  5. iOS - 上架的APP 生成二维码下载

    1.首先打开苹果App Store商店进入到里面,找到需要打开链接地址的应用程序,例如:百度. 2. 在App Store商店里面先点击一下应用程序图标,再按一下…分享按钮. 3. 接着选择分享APP ...

  6. IIS支持IPA、APK文件的下载

    IIS里MIME类型中默认是没有ipa,apk文件的,所以无法直接通过网络下载.   解决方法如下: 1.打开IIS信息服务管理器,选中自已的网站,在右边面板中找到MIME类型. 2.双击打开,点击右 ...

  7. 【深度好文】多线程之WaitHandle-->派生-》Semaphore信号量构造

    Semaphore 继承自WaitHandle. 信号量说简单点就是为了线程同步,或者说是为了限制线程能运行的数量. //创建一个限制资源类 //资源数为5,开放资源数为2 //主线程自动占有3个资源 ...

  8. 面向对象设计模式纵横谈:Bridge 桥接模式(笔记记录)

    桥接模式是一个比较难理解的设计模式,设计和分析的时候也不容易把握,咱们听听“李建忠”老师是怎么来讲的.我们还是从演变的角度来说问题,一步一步的来把问题说清楚.先谈谈“抽象”和“实现”的关系. 抽象与实 ...

  9. Laravel 5 如何对部份 URI 禁用 CSRF 验证

    打开中间件 VerifyCsrfToken.php 在其 $except 属性中添加要禁用的 uri,如: api/user/add api/user/* api/*

  10. CSV 文件

    CSV 文件 CSV(Comma Separated Values 逗号分隔值) 是一种文件格式(如.txt..doc等),也可理解 .csv 文件就是一种特殊格式的纯文本文件.即是一组字符序列,字符 ...