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. PHP学习笔记二十一【全局变量】

    <?PHP //定义全局变量 global $a; $a=9; //给全局变量赋值 function test1() { global $a; $a=45; } test1(); echo $a ...

  2. PHP自动生成前端的表单框架

    <?php /** * 为当前所在菜单项样式 * @param string $controller_name * @param string $action_name * @param str ...

  3. Web程序工作原理

    1.Web程序工作原理 (1)Web一词的含义 Network:[计算机]电脑网络,网 Web:[计算机]万维网(World Wide Web),互联网(Internet) Web程序,顾名思义,即工 ...

  4. Python学习(五) Python数据类型:列表(重要)

    列表: list是一组有序项目的数据结构. 列表是可变类型的数据,列表用[]进行表示,包含了多个以","分隔的项目. list=[] type(list) //<type ' ...

  5. mysqlbackup 备份失败的分析

    现象: 1.从mysqlbackup 的日志上来看是它一直处于state: Waiting for locks; 2.从mysql 层面show processlist 上看它的处于waiting f ...

  6. Js函数加括号、不加括号(转)

    函数只要是要调用它进行执行的,都必须加括号.此时,函数()实际上等于函数的返回值.当然,有些没有返回值,但已经执行了函数体内的行为,这个是根本,就是说,只要加括号的,就代表将会执行函数体代码. 不加括 ...

  7. stopWeblogic时提示错误以及无法关闭服务

    执行: y@y:~/oracle/middleware/user_projects/domains/yshy_domain/bin$ ./stopWebLogic.sh 错误信息如下: Stoppin ...

  8. winform datagridview 添加行号。

    private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { private ...

  9. lisp构造表

    CONS 操作符 我们刚刚学习了如何拆分一个表,现在学习如何合并一个表. CONS 操作符就是做这件事情的. 假设有一个列表 (1 2 3) ,我们做一下 CAR 操作: (car '(1 2 3)) ...

  10. c3p0链接池

    频繁的链接数据库是非常消耗性能的,所以就采用了将一定量的链接保存在一个池中,这个池我们叫做链接池. 详细请看:http://baike.baidu.com/link?url=dlTW-fTS3N_-j ...