题目链接

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

题解:题目描述什么的一大段废话,直接看输入输出,发现是一个经典的DFS连通块问题。

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
char mp[][];
int n,m;
int ans;
int dir[][]={{,},{,-},{,},{-,}};
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
int nx=x+dir[i][],ny=y+dir[i][];
if(nx>=&&nx<n&&ny>=&&ny<m)
{
if(mp[nx][ny]=='#')
{
mp[nx][ny]='.';
dfs(nx,ny);
}
}
}
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
//Start
int N;
cin>>N;
while(N--)
{
cin>>n>>m;
msp,ans=;
for(int i=; i<n; i++)cin>>mp[i];
for(int i=; i<n; i++)
for(int j=; j<m; j++)
if(mp[i][j]=='#')
{
mp[i][j]='.';
ans++;
dfs(i,j);
}
printf("%d\n",ans);
}
return ;
}

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

  1. hdu 2952 Counting Sheep

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

  2. HDU2952:Counting Sheep(DFS)

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

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

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

  4. HDU - 5952 Counting Cliques(DFS)

    A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a ...

  5. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

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

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

  7. HDU-2952 Counting Sheep (DFS)

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

  8. ACM HDU-2952 Counting Sheep

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

  9. HDU 3046 Pleasant sheep and big big wolf(最小割)

    HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...

随机推荐

  1. Netty(7)源码-ByteBuf

    一.ByteBuf工作原理 1. ByteBuf是ByteBuffer的升级版: jdk中常用的是ByteBuffer,从功能角度上,ByteBuffer可以完全满足需要,但是有以下缺点: ByteB ...

  2. python3中字典的copy

    字典是可变的: first和second同时指向一个字典.first修改也会影响second.在程序中一定注意对字典参数的修改会对原始的字典进行修改.这也体现了字典是可变的. 字典的copy方法是浅拷 ...

  3. 设计模式 -- 代理模式 (Proxy Pattern)

    定义: 为其他对象提供一种代理以控制对这个对象的访问: 角色: 1,抽象主题类,(接口或者抽象类),抽象真实主题和代理的共有方法(如下Subject类): 2,具体实现的主题类,继承或者实现抽象主题类 ...

  4. Java 年月日 日期加减

    public static String DATE_YEAR="YEAR";//年 public static String DATE_MONTH="MONTH" ...

  5. Rosenbrock function

    w测试最优化算法性能可通过其. https://en.wikipedia.org/wiki/Rosenbrock_function https://zh.wikipedia.org/wiki/Rose ...

  6. Java面试题及答案(基础122道,编码19道)

    JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...

  7. php笔记(二)PHP类和对象之Static静态关键字

      PHP类和对象之Static静态关键字 静态属性与方法可以在不实例化类的情况下调用,直接使用类名::方法名的方式进行调用.静态属性不允许对象使用->操作符调用. class Car { pr ...

  8. web端和手机端测试有什么不同

    面试中经常被问到web端测试和手机端测试有什么相同点和区别呢?现在总结一下这个问题,如有不对敬请指正 web端和手机端测试有什么区别 1.相同点 不管是web测试还是手机App测试,都离不开测试的相关 ...

  9. 《HTML5与CSS3权威指南》读书笔记(上册)—HTML5篇

    豆瓣上的评分少且评价不太好,阅读当中发现几处刊物上的小问题,不过线下口碑貌似不错,基本上人手一本 上册五百多页,主讲H5,分为标签,本地存储,离线应用程序,新的API,获取地理位置信息标签包含表单,C ...

  10. 用PhotoSwipe制作相册,手势可放大

    1.引入css和js <link href="css/photoswipee.css" rel="stylesheet" type="text/ ...