Counting Sheep

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2060    Accepted Submission(s): 1359

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
 
 
值得注意的是:bfs()函数里面定义的参数一定要定义成全局变量,如果用C语言的scanf来输入数据,要用getchar()来清除缓存区的'\n'。
 
 #include<iostream>
using namespace std; int arr[][]={,,,,,-,-,}; //分四个方向搜索
char sheep[][];
int T,i,j,sum,h,w; //搜索羊群
void bfs(int a,int b)
{
if(sheep[a][b]=='.') return; //遇到 '.'返回
if(a<||b<||a>=h||b>=w) return; //数组越界返回
sheep[a][b]='.'; //记录,将每次找到的羊群变成'.',下次循环直接跳过
for(int i=;i<;i++)
bfs(a+arr[i][],b+arr[i][]); //找到每只羊以后向四个方向搜索
} int main()
{
cin>>T;
while(T--)
{
cin>>h>>w;
for(i=;i<h;i++)
for(j=;j<w;j++)
cin>>sheep[i][j]; //此处利用c++的输入方法,不用考虑缓冲区换行符的影响
sum=;
for(i=;i<h;i++)
for(j=;j<w;j++)
{
if(sheep[i][j]=='#')
{
++sum; //用sum来记录每次找到的羊群数量
bfs(i,j);
}
}
cout<<sum<<endl;
}
return ;
}

ACM HDU-2952 Counting Sheep的更多相关文章

  1. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

  2. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  3. hdu 3046 Pleasant sheep and big big wolf 最小割

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...

  4. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  6. hdu 5862 Counting Intersections

    传送门:hdu 5862 Counting Intersections 题意:对于平行于坐标轴的n条线段,求两两相交的线段对有多少个,包括十,T型 官方题解:由于数据限制,只有竖向与横向的线段才会产生 ...

  7. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  8. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  9. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. ubuntu16上安装openJDK.md

    ubuntu16上安装openJDK.md 环境 操作系统:ubuntu 16.04.2 LTS 安装 当你不需要安装oracle的JDK时,使用openJDK,安装就比较方便. sudo apt-g ...

  2. jar包读取配置文件

    读取jar包内配置文件: Properties config = new Properties(); InputStream in = this.getClass().getClassLoader() ...

  3. js创建类(封装)

    js如何创建类(封装)     学过其他面向对象语言的JavaScripter,可能都应用过类,如:class{},等定义的一系列方法, 但是初学者看是学习js的时候,经常会看到这样一句话,那就是Ja ...

  4. Java生成二维码和解析二维码URL

    二维码依赖jar包,zxing <!-- 二维码依赖 start --><dependency> <groupId>com.google.zxing</gro ...

  5. 【response-下载文件步骤】

    Java中都通用文件下载(ContentType.文件头.response.out四步骤) 1.设置文件ContentType类型 2.设置文件头 3.通过response获取ServletOutpu ...

  6. wcf datetime json format

    wcf 内置的json序列化工具,有时需要替换,或者特殊情况的处理,需要修改. 我也遇到了Dto属性类型是datetime,json的反序列化 和 序列号不友好. 这是国外网站的一个方案:Replac ...

  7. Expression Blend实例中文教程(7) - 动画基础快速入门Animation

    通过前面文章学习,已经对Blend的开发界面,以及控件有了初步的认识.本文将讲述Blend的一个核心功能,动画设计.大家也许注意到,从开篇到现在,所有的文章都是属于快速入门,是因为这些文章,都是我曾经 ...

  8. .net面试题升级版

    1.列举ASP.NET页面之间传值的几种方式. 答:使用QueryString,如.../id=1;response.Redirect() 使用Session 使用Server.Transfer 使用 ...

  9. type=file的change事件只能执行一次的解决方案

    最近帮朋友做个项目中遇到了type=file change事件只能执行一次的问题,度娘了一下,发现提供了各种解决方案,所以决定记录一下我的思考方向和最终解决方式. 起初帮朋友做个项目,项目中遇到上传文 ...

  10. [android] 练习viewpagerindicator的使用(一)

    主要是学习一下使用这个库 activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...