Counting Sheep

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

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

Source

IDI Open 2009
 #include <stdio.h>
#include <string.h>
char map[][];
int f[][] = {,,-,,,,,-}; //注意定义方向数组。只有四个方向上下左右。
int n,m,s;
void dfs(int x,int y)
{
int i;
int x1,y1;
for(i = ;i<;i++)
{
x1= x+f[i][];
y1 = y+f[i][];
if(x1< || y1< || x1>=n || y1>=m || map[x1][y1]!='#')
continue;
map[x1][y1] = '.';
dfs(x1,y1);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int i,j,l;
s = ;
for(i = ;i<n;i++)
{
scanf("%s",map[i]);
}
for(i = ;i<n;i++)
{
for(j = ;j<m;j++)
{
if(map[i][j] == '#')
{
s++;
map[i][j] = '.';
dfs(i,j);
}
}
}
printf("%d\n",s);
} return ;
}

HDU-2952 Counting Sheep (DFS)的更多相关文章

  1. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  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. 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)

    题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...

  4. hdu 1716 排序2(dfs)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 3887 Counting Offspring(DFS序【非递归】+树状数组)

    题意: N个点形成一棵树.给出根结点P还有树结构的信息. 输出每个点的F[i].F[i]:以i为根的所有子结点中编号比i小的数的个数. 0<n<=10^5 思路: 方法一:直接DFS,进入 ...

  6. HDU 3887 Counting Offspring(DFS序+树状数组)

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

  7. hdu 2660 Accepted Necklace(dfs)

    Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...

  8. hdu 2952 Counting Sheep

    本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...

  9. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. HDU 2501 Tiling_easy version(简单递推)

    Tiling_easy version Problem Description 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有 ...

  2. Object-API-NSLog

    NSLog中的基础数据类型 输出格式: NSLog("") char: %c short int: %hi %hx %ho unsigned short int: %hu %hx ...

  3. IOS代理

    之前看过一些关于代理的资料,始终感觉还是很模糊,最近抽出一段空闲的时间,将这块内容整理下: 什么是代理,顾名思义就是帮别人做些事情,比如买房子,当我们买房子时,我们一般会找房屋中介,因为他们就是干这件 ...

  4. 安装grid之前检查配置 ,报错如下

    centos 5 _x86_64 oracle 11.2 安装grid之前检查配置 ,报错如下 : ./runcluvfy.sh stage -pre crsinst -n rac1,rac2 -fi ...

  5. js判断是否全是相同的字符串

    isSameStr("aa2a") //不都是相同的字符 function isSameStr(str){ var tem=0; for(var i=0;i<str.leng ...

  6. linux进程间通信--无名管道

    管道 只能用于具有亲缘关系的进程之间通信是一个半双工的通信模式, 具有固定的写读端和写端,管道可以看成一种特殊的文件,对它可以使用普通的read.write等操作 管道的创建: #include &l ...

  7. IE8+等兼容、360调用webkit内核小记

    首先是处理IE8.9等的兼容问题,注意以下几点: 1,尽可能严格要求自己使用w3c推荐的方式编写html/css 2,在html页面顶部添加<!DOCHTML html>,不清楚请查看参考 ...

  8. php hook 之简单例子

    <?php// 应用单例模式// 建立相应的 plugins 文件夹,并建立 .php 文件放在里面class plugin{    public $actions;    public $fi ...

  9. css3动画使用技巧之—JQ配合css3实现轮播之animation-delay应用

    <!DOCTYPE html> <html> <head> <title>css3动画使用技巧之—JQ配合css3实现轮播之animation-dela ...

  10. CentOS使用sudo提示用户不在sudoers文件中的解决方法

    1切换到root用户[linux@localhost ~]$ su root密码:[root@localhost ~]# 2查看/etc/sudoers文件权限,如果只读权限,修改为可写权限 [roo ...