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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath> using namespace std; bool mp[][];
char s[];
bool visit[][];
int q[][],l,r;
int d1[] = {,-,,};
int d2[] = {,,,-}; void bfs(int a,int b)
{
l = r = ;
visit[a][b] = ;
q[r][] = a,q[r++][] = b;
while(l<r)
{
int x = q[l][],y = q[l++][];
for(int j = ;j<;j++)
{
int xx = x+d1[j],yy = y+d2[j];
if(mp[xx][yy]&&!visit[xx][yy])
{
visit[xx][yy] = ;
q[r][] = xx,q[r++][] = yy;
}
}
}
} int main()
{
int z,n,m,i,j,k;
cin>>z;
while(z--)
{
cin>>n>>m;
memset(mp,,sizeof(mp));
memset(visit,,sizeof(visit));
for(i = ;i<=n;i++)
{
scanf("%s",s);
for(j = ;j<m;j++)
{
mp[i][j+] = (s[j] == '#')?:;
}
}
int ans = ;
for(i = ;i<=n;i++)
{
for(j = ;j<=m;j++)
{
if(!visit[i][j]&&mp[i][j]) ans++,bfs(i,j);
}
}
cout<<ans<<endl;
}
return ;
}

hdu2952Counting Sheep的更多相关文章

  1. 2001. Counting Sheep

      After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...

  2. 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 ...

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

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

  4. Counting sheep...

    Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...

  5. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  6. Funny Sheep(思维)

    Problem 1606 - Funny Sheep Time Limit: 1000MS   Memory Limit: 65536KB    Total Submit: 612  Accepted ...

  7. HDU 3046 Pleasant sheep and big big wolf(最小割)

    HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...

  8. HDU 2952 Counting Sheep(DFS)

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

  9. HDU2952:Counting Sheep(DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

随机推荐

  1. Android 使用开源xUtils来实现多线程下载(非原创)

    1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间 2.导包:xUtils-2.6 ...

  2. Java---文件夹及文件操作

    /** * 获取文件夹大小 * @param file File实例 * @return long */ public static long getFolderSize(java.io.File f ...

  3. Java学习——内部类

    内部类的访问规则:1,内部类可以直接访问外部类中的成员,包括私有. 之所以可以直接访问外部类中的成员,是因为内部类中持有了 一个外部类的引用,格式 外部类名.this2,外部类要访问内部类,必须建立内 ...

  4. (整理)ubuntu 的 相关知识(来自 鸟哥的私房菜)

    1. Linux 文件权限概念 $ ls 察看文件的指令 $ ls -al 出所有的文件详细的权限与属性 (包含隐藏档,就是文件名第一个字符为『 . 』的文件) 在你第一次以root身份登入Linux ...

  5. 如何判断CPU的位数

    CPU是16位,32位,还是64位,主要指的是数据总线(data bus)有多少位,16位数据总线表示CPU一次可以从内存取2个byte的数据,32位数据总线表示CPU一次可以从内存取4byte数据, ...

  6. XHTML 结构化:使用 XHTML 重构网站

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  7. JavaScript 全局变量命名空间生成函数

    <script type="text/javascript"> var GLOBAL = {}; GLOBAL.namespace = function(str){ v ...

  8. php5.5新特性之yield理解

    今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...

  9. php 写队列

    这里不得不提到php的数组函数真的是太强大了 队列是先进先出 那么对于数组来说就尾部插入,头部拿出 这里提供方法 尾部插入 我们知道有一个函数array_push 头部拿出 array_shift($ ...

  10. 基于cygwin构建u-boot(一)环境搭建

    从本文开始,更系统的描述基于cygwin构建u-boot. 之前<痛苦的版本对齐>系列,对于cygwin环境下Sourcery CodeBench,u-boot-1.1.6的构建有侧面的说 ...