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. Cesium的api之关于viewer(二)

    1.构建一个viewer,如下创建:options的参数根据实际情况,进行设定 var viewer = new Cesium.Viewer('cesiumContainer', { //Start ...

  2. PHP学习笔记(八)

    关于PHP中的缓存函数ob_start() and ob_end_flush(). PHP输出机制:输出内容->缓存->输出到浏览器.ob_start(callback function) ...

  3. 16_AOP入门准备_动态代理模式

    [工程截图] [PersonDao.java] package com.HigginCui.daoProxy; //目标类接口 public interface PersonDao { public ...

  4. 数学符号π (Pi)、Σ(Capital Sigma)、μ (Mu) 、σ(sigma)、∏(capital pi), ∫(Integral Symbol)的来历

    1.π (Pi; periphery/周长) March 14 marks Pi Day, the holiday commemorating the mathematical constant π ...

  5. 无责任共享 Coursera、Udacity 等课程视频

    本文转载自网络,原作者不详. (本文是用 markdown 写的,访问 https://www.zybuluo.com/illuz/note/71868 获得更佳体验) 程序语言 interactiv ...

  6. BSTR共享内存问题

    BSTR bstrName = OLESTR("Test String"); BSTR b1 = bstrName; BSTR b2 = bstrName; bstrName = ...

  7. MySQL的基本使用

    SQL     DDL:数据定义语言  CREATE DROP ALTER     DML:数据操作语言  SELECT INSERT UPDATE DELETE     DCL:数据控制语言  GR ...

  8. thinkphp 整合 ucenter

    http://xcodebox.com/2013/06/8855.html 参考 1   ucenter源码目录下 /advanced/examples/api目录 copy到thinkphp项目根目 ...

  9. [转]PHP Session原理分析及使用

    之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...

  10. POJ 2531 Network Saboteur 位运算子集枚举

    题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...