Counting Sheep

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 24

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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
#include <iostream>
#include<cstdio>
#include<cstring>
#include<deque>
using namespace std; struct node
{
int x,y;
};
int dr[][]={{,},{,},{-,},{,-}};
deque<node> s;
int i,j,n,m,num,t;
char ch[][]; void bfs(int x,int y)
{
node t;
t.x=x;
t.y=y;
ch[x][y]='*';
s.push_back(t);
while(!s.empty())
{
node p=s.front();
for(int i=;i<;i++)
{
int xx=p.x+dr[i][];
int yy=p.y+dr[i][];
if (xx>= && xx<n && yy>= && yy<m && ch[xx][yy]=='#')
{
t.x=xx;
t.y=yy;
ch[xx][yy]='.';
s.push_back(t);
}
}
s.pop_front();
}
return;
}
int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d%d",&n,&m);
num=;
for(i=;i<n;i++)
scanf("%s",&ch[i]);
for(i=;i<n;i++)
for(j=;j<m;j++)
if (ch[i][j]=='#')
{
bfs(i,j);
num++;
}
printf("%d\n",num);
}
return ;
}

HDU2952:Counting Sheep(DFS)的更多相关文章

  1. HDU-2952 Counting Sheep (DFS)

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

  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. Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏

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

  4. ACM HDU-2952 Counting Sheep

    Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

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

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

  6. Counting sheep...

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

  7. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  8. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

随机推荐

  1. digitalocean教程:你应该知道的10件事

    DigitalOcean VPS性价比高,全球拥有多处机房,无须备案,非常适合守规矩的中国用户使用.digitalocean优惠码.digitalocean速度.digitalocean评测是网友关心 ...

  2. error LNK2001:unresolved external symbol "xxx:static xxx"

    一般是在类的头文件里定义了某个static变量而没有在类的 Implemention 里去初始化(也许不是这么称呼)这个变量. 比如在 xxdlg.h 中写了如下代码 1: class CxxDlg ...

  3. 第三次冲刺spring会议(第六次会议)

    [例会时间]2014/5/25 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇

  4. 关于ios 程序加载百度地图lib,出现链接错误:找不到符号 (null): _OBJC_CLASS_$_BMKMapManager的解决办法

    报告的错误信息 ld: warning: ignoring file /Users/5012/Documents/sphuang/IOS_project/baidu_map/ShareLocation ...

  5. ural 1356. Something Easier(数论,哥德巴赫猜想)

    1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...

  6. ios的虚拟键盘与fixed移动端的bug

    //$('#search')表单input;$('.search_out')浮动元素 var u = navigator.userAgent, app = navigator.appVersion;v ...

  7. 7.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。

    package com.bao; public class Print1 { int x; void output() { if(x==1) { System.out.println("AB ...

  8. dfs + 最小公倍数 Codeforces Round #383 (Div. 2)

    http://codeforces.com/contest/742/problem/C 题目大意:从x出发,从x->f[x] - > f[f[x]] -> f[f[f[x]]] -& ...

  9. myBatIs.Net 调用Oracle 存储过程返回游标

    找了好久,网上也没示例,全是java的,没办法,后来看到一个网上别人写的例子. http://www.myfirm.cn/blog/article/Control/13.html 上面照套还是出错,我 ...

  10. FZU 1502 Letter Deletion

    最长公共子序列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...