题目链接

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. 最简单的MFC

    #include <SDKDDKVer.h> #include <afxwin.h> #include <afxext.h> #include <iostre ...

  2. 面向对象---java代码块

    概念:代码块是指用{}括起来的一段代码. 根据位置及声明的关键字不同,代码块可分为普通代码块.构造块.静态代码块.同步代码块4种. 1.普通代码块: 直接在方法中或在语句中定义 public clas ...

  3. linux shell脚本学习xargs命令使用详解

    作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题 xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处 ...

  4. 安卓平台多个视频叠加演示demo说明

    多个音视频编辑演示说明: 第一个-----字幕和视频的叠加: 说明: 把字幕文件中的文字,按照时间叠加到视频上去,形成新的视频. 类似我们看电影时的字幕. 下载地址:http://www.cnblog ...

  5. 给RelativeLayout设置背景,无效果bug解决

    drawable文件夹下面 tomyshop_selector.xml文件 <?xml version="1.0" encoding="utf-8"?&g ...

  6. PHP 在 Nginx 下主动断开连接 Connection Close 与 ignore_user_abort 后台运行

    这两天弄个PHP调用 SVN 同步 update 多台服务器更新的程序,为了避免 commit 的时候不会被阻塞卡半天得想个办法只请求触发,而不需要等待程序 update 完成返回结果这样耗时太长,所 ...

  7. Qt 学习笔记

    继承自QObject 的Qt类都具有支持信号和槽的能力,并且在子类的实现代码中直接使用connect()函数 pwdLineEdit->setEchoMode(QLineEdit::Passwo ...

  8. WPF 限制Textbox输入的内容

    限制文本框TextBox的输入内容,在很多场景都有应用.举个例子,现在文本框中,只能输入0.1.2.3.4.5.6.7.8.9.“|”这11个字符. 限制输入0-9很容易实现,关键是这个“|”符号.它 ...

  9. 《C程序设计语言》 squeeze函数(从字符串s中删除字符c)

    squeeze void squeeze(char string[], int ch) { int i, j; ; string[i] != '\0'; i++) { if (string[i] != ...

  10. ASUS S46CB 刷BIOS

    1. 从ASUS官网下载要新的BIOS文件: 地址:https://www.asus.com.cn/Notebooks_Ultrabooks/S46CB/HelpDesk_Download/ 2. 开 ...