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. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  2. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 视图Nunjucks

    视频地址:https://www.cctalk.com/v/15114923888328 视图 Nunjucks 彩虹是上帝和人类立的约,上帝不会再用洪水灭人. 客户端和服务端之间相互通信,传递的数据 ...

  3. System.Data.SqlClient.SqlError: 尚未备份数据库的日志尾部

    SQL还原时出现下面的错误,System.Data.SqlClient.SqlError: 尚未备份数据库 "***" 的日志尾部.如果该日志包含您不希望丢失的工作,请使用 BAC ...

  4. Electron mouse events 参数解析

    1.https://electronjs.org/docs/api/web-contents 2.通常用: monitorEvents(document.body, 'mouse')  检测正常的值: ...

  5. 多序列比对后可视化之texshade

    一 :准备工作 1)中文的 LaTeX - CTEX 2)LaTeX 的*.tex 的编辑工具-Texmarker 3)TEXshade宏包 二:安装 1) 直接双击运行下载的CTEX,安装过程中,可 ...

  6. Spring boot集成 MyBatis 通用Mapper

    配置 POM文件 <parent> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  7. mysql物理备份

    原本以为直接将data文件夹下每个数据库对应的文件夹拷贝到新的MySQL的data文件夹就可以了,其实不然. 这样做有几个问题: 1.如果是用了引擎的表,还需要复制ibdata文件,并且frm文件所在 ...

  8. date.getTime()

    Date date = new Date(); System.out.println(date.getTime()); 输出结果是1210745780625 编译时间当时时间大概是2008年5.14好 ...

  9. 《Visual C++开发实战1200例 第1卷》扫描版[PDF]

    [内容简介:] <Visual C++开发实战1200例(第1卷)>是“软件开发实战1200例”丛书之一.<Visual C++开发实战1200例(第1卷)>,编程实例的四库全 ...

  10. Resume (Curriculum Vitae)

    The resume (Curriculum Vitae) is a selling tool outlining your skills and experience so an employer ...