HDU 2952 Counting Sheep(DFS)
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.
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.
Notes and Constraints
0 < T <= 100
0 < H,W <= 100
4 4
题解:题目描述什么的一大段废话,直接看输入输出,发现是一个经典的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)的更多相关文章
- hdu 2952 Counting Sheep
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=2952 题意:上下左右4个方向为一群.搜索有几群羊 #include <stdio.h> # ...
- HDU2952:Counting Sheep(DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 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 ...
- HDU - 5952 Counting Cliques(dfs搜索)
题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU-2952 Counting Sheep (DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- ACM HDU-2952 Counting Sheep
Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3046 Pleasant sheep and big big wolf(最小割)
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...
随机推荐
- GIT 代码管理工具 SourceTree
什么是git? git是一款开源的分布式版本控制工具 在世界上所有的分布式版本控制工具中,git是最快.最简单.最流行的 git的起源 作者是Linux之父:Linus Benedict Torval ...
- asp.net MVC漏油配置总结
URL构造 命名参数规范+匿名对象 routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}&qu ...
- C# Where
判断一个字符串中是否包含字符串数组里的字符,恶意字符限制提交,一般人,包括最初的我,会这样写 public bool ValidateStr(string[] parms) { bool result ...
- 生成 Qt 文档
个人总结 从命令行进入Qt安装目录 设置环境变量 set path=D:/mingw32/bin;D:/Qt/5.0.0/qtbase/bin;D:/icu/bin;D:/icu ...
- mysql 中文字段排序( UTF8按拼音首字母排序)
select * from tableName CONVERT( FieldName USING gbk ) COLLATE gbk_chinese_ci desc
- html的简单表单制作...day5 php
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 什么是Bash Shell的内建(build in)命令
1.什么是build in命令: shell内建命令是指bash(或其它版本)工具集中的命令.一般都会有一个与之同名的系统命令,比如bash中的echo命令与/bin/echo是两个不同的命令,尽管他 ...
- from表单实现无跳转上传文件,接收页面后台数据。
本文基于我刚写的http://www.cnblogs.com/iwang5566/p/6287529.html进行了简单的扩展,实现页面无跳转表单数据提交,并接收后台返回的数据. 下载好,上一篇文章d ...
- Maven pom项目部署
maven控制台运行程序 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec- ...
- 1.1 Eclipse下载安装
可直接上官网下载:http://www.eclipse.org/downloads/ 直接下载地址:http://www.eclipse.org/downloads/download.php?file ...