ACM HDU-2952 Counting Sheep
Counting Sheep
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2060 Accepted Submission(s): 1359

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.
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.
Notes and Constraints 0 < T <= 100 0 < H,W <= 100
4 4

#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的更多相关文章
- hdu 2952 Counting Sheep
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...
- HDU 2952 Counting Sheep(DFS)
题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...
- 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 ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5862 Counting Intersections
传送门:hdu 5862 Counting Intersections 题意:对于平行于坐标轴的n条线段,求两两相交的线段对有多少个,包括十,T型 官方题解:由于数据限制,只有竖向与横向的线段才会产生 ...
- HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...
- 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 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
随机推荐
- ubuntu16上安装openJDK.md
ubuntu16上安装openJDK.md 环境 操作系统:ubuntu 16.04.2 LTS 安装 当你不需要安装oracle的JDK时,使用openJDK,安装就比较方便. sudo apt-g ...
- jar包读取配置文件
读取jar包内配置文件: Properties config = new Properties(); InputStream in = this.getClass().getClassLoader() ...
- js创建类(封装)
js如何创建类(封装) 学过其他面向对象语言的JavaScripter,可能都应用过类,如:class{},等定义的一系列方法, 但是初学者看是学习js的时候,经常会看到这样一句话,那就是Ja ...
- Java生成二维码和解析二维码URL
二维码依赖jar包,zxing <!-- 二维码依赖 start --><dependency> <groupId>com.google.zxing</gro ...
- 【response-下载文件步骤】
Java中都通用文件下载(ContentType.文件头.response.out四步骤) 1.设置文件ContentType类型 2.设置文件头 3.通过response获取ServletOutpu ...
- wcf datetime json format
wcf 内置的json序列化工具,有时需要替换,或者特殊情况的处理,需要修改. 我也遇到了Dto属性类型是datetime,json的反序列化 和 序列号不友好. 这是国外网站的一个方案:Replac ...
- Expression Blend实例中文教程(7) - 动画基础快速入门Animation
通过前面文章学习,已经对Blend的开发界面,以及控件有了初步的认识.本文将讲述Blend的一个核心功能,动画设计.大家也许注意到,从开篇到现在,所有的文章都是属于快速入门,是因为这些文章,都是我曾经 ...
- .net面试题升级版
1.列举ASP.NET页面之间传值的几种方式. 答:使用QueryString,如.../id=1;response.Redirect() 使用Session 使用Server.Transfer 使用 ...
- type=file的change事件只能执行一次的解决方案
最近帮朋友做个项目中遇到了type=file change事件只能执行一次的问题,度娘了一下,发现提供了各种解决方案,所以决定记录一下我的思考方向和最终解决方式. 起初帮朋友做个项目,项目中遇到上传文 ...
- [android] 练习viewpagerindicator的使用(一)
主要是学习一下使用这个库 activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...