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是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...
随机推荐
- mysql的部分基础知识....
- jquery 获取当前对象的id取巧验证的一种方法
<!doctype html><html><head><meta charset="utf-8"><title>titl ...
- jquery验证表单是否满足正则表达式是否通过验证例子
//验证通用函数 a表示元素对象,b表示正则表达式,c存bool值 function testyz(a,b,c){ c=false; $(a).on("blur",function ...
- November 12th 2016 Week 46th Saturday
Never love anyone who treats you like you are ordinary. 请爱那些爱你的人. Don't waste your limited energy on ...
- .Net中jQuery.ajax()调用asp.net后台方法 总结
利用JQuery的$.ajax()调用.Net后台方法有多种方式, 不多说了 直接上代码 前台代码 <script type="text/javascript"> $ ...
- Regmon7.04绿色版(注册表变动实时监视工具)
Regmon V7.04 简繁中文绿色版 软件大小: 155KB软件语言: 简体中文运行环境:Win9x/NT/2000/XP/2003/软件类别:国外软件 / 免费版 / 系统其它Regmon Re ...
- Dreamweaver层使用八定律
当然,这些并非真正的定律,而只是一些有益的忠告,使你免陷于使用层时可能的困顿中.原来有九条定律的,我们精简掉一条,还有下面的八条: 1. 如果你要嵌套层,决不要使用多重父层,应共享一个共同的单一父层. ...
- Flash cc 添加目标Flash Player
原文出处:http://zengrong.net/post/1568.htm 第一步 首先下载最新的 playerglobal.swc(基于Flash Player11): http://www.ad ...
- HDOJ-1051 Wooden sticks(贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- SQLServer性能优化
http://www.cnblogs.com/studyzy/archive/2008/11/24/1339772.html