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. Oracle更改字符集

    更改oracle的字符集: sqlplus / as sysdba SQL> shutdown immediate; Database closed. Database dismounted. ...

  2. iPad学做菜

    项目描述:家常菜.川菜 .鲁菜.东北菜.甜品等各大菜系应有尽有,详细的制作步骤,再也不用为自己不会做饭而烦恼. 主要技术:主界面采用UISplitViewController的结构设计:自定义各大菜系 ...

  3. struts 学习之问一

    今天在进行struts全局类型和局部类型转换时,发现一个问题,如下: 当输入一个点的坐标时,我使用全局转换提示错误,找不到类,当改变成局部类型转换时,可以成功转换,不知道这个是什么原因,难道全局不可以 ...

  4. c++ 指针的简单用法

    对于指针,其实只需要明白几点就可以. 1.指针,是一个数值为地址的变量,这里尤其注意,指针变量的值是地址!就是例如40002这种像门牌号的地址值,其实就是内存中的一个编号. 2.&,该符号的意 ...

  5. Stripies(POJ 1862 贪心)

    Stripies Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14151   Accepted: 6628 Descrip ...

  6. GTW likes math(BC 1001)

    GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...

  7. MySQL--query-cache

    知识准备: 1.mysql 的query-cache是什么?  mysql可以把执行完成的select 语句和这个select 语句对应的结果集缓存起来:下次再用调用相同的select 语句时就直接返 ...

  8. python模范发送邮件的时候,才smtp.connect的时候总是抛出错误

    python发送邮件的时候,总是出现:[Errno 10060] 错误码 根据debug得到在connect的时候出错. 认真检查了下host,没有错呀~应该就是服务器的host. 查看了下网上的一些 ...

  9. mysql null值转换

    1.如果为空返回0 select ifnull(null,0) 2.如果为空返回0,否则返回1 select if(isnull(col),0,1) as col.

  10. 理解JMS规范中消息的传输模式和消息持久化

    http://blog.csdn.net/aitangyong/article/category/2175061 http://blog.csdn.net/aitangyong/article/det ...