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. 使用solr模拟京东搜素功能

    1 项目需求 1.可以根据关键字搜索商品 2.可以根据商品的分类和价格过滤搜索结果 3.可以根据价格排序 4.可以实现基本的分页功能 2 界面效果 3 项目环境搭建 1.创建一个动态的web工程 2. ...

  2. 【javascript/css】关于鼠标事件onmousexxx和css伪类hover

    在运用鼠标移入移出事件时,一般有两种做法,一种是DOM事件的"onmouseover"和"onmouseout",还有一种是css的伪类":hover ...

  3. JS原型与原型链(好文看三遍)

    一. 普通对象与函数对象 JavaScript 中,万物皆对象!但对象也是有区别的.分为普通对象和函数对象,Object ,Function 是JS自带的函数对象. 下面举例说明: function ...

  4. 解决MysqlWorkbench Export Data时报错:'delayed-insert'=FALSE

  5. sql语句将数字转为汉字展示

    select [表字段Name] , ( case [表字段OrderState] when 1 then '已核销' when 2 then '确认前的移动端取消'when 3 then '已完成' ...

  6. Silverlight & Blend动画设计系列六:动画技巧(Animation Techniques)之对象与路径转化、波感特效

    当我们在进行Silverlight & Blend进行动画设计的过程中,可能需要设计出很多效果不一的图形图像出来作为动画的基本组成元素.然而在设计过程中可能会出现许多的问题,比如当前绘制了一个 ...

  7. SEO学习

    一.什么是SEO SEO是由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”!SEO是指通过对网站进行站内优化(网站结构调整.网站内容建设.网站代码优化等 ...

  8. 一:SpringIOC&DI

    一:spring 1.spring介绍 spring负责管理项目中的所有对象,看作是项目中对象的管家. spring一站式框架: spring框架性质是属于容器性质的 容器中装什么对象就有什么功能,所 ...

  9. centos下如何停止ping命令

    ctrl + c 或者 Ctrl + d(好像不行) man ping

  10. 解决JVM内存溢出问题

    今天遇到了一个问题,当我在增加配置文件(*.xml)内容的时候,重新启动tomcat6时,控制台报错:java.lang.StackOverflowError: 即,栈溢出错误. 内存溢出,即程序运行 ...