HDU2952:Counting Sheep(DFS)
Counting Sheep
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 35 Accepted Submission(s) : 24
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description

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
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
Notes and Constraints
0 < T <= 100
0 < H,W <= 100
Sample Input
2
4 4
#.#.
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###
Sample Output
6
3
#include <iostream>
#include<cstdio>
#include<cstring>
#include<deque>
using namespace std; struct node
{
int x,y;
};
int dr[][]={{,},{,},{-,},{,-}};
deque<node> s;
int i,j,n,m,num,t;
char ch[][]; void bfs(int x,int y)
{
node t;
t.x=x;
t.y=y;
ch[x][y]='*';
s.push_back(t);
while(!s.empty())
{
node p=s.front();
for(int i=;i<;i++)
{
int xx=p.x+dr[i][];
int yy=p.y+dr[i][];
if (xx>= && xx<n && yy>= && yy<m && ch[xx][yy]=='#')
{
t.x=xx;
t.y=yy;
ch[xx][yy]='.';
s.push_back(t);
}
}
s.pop_front();
}
return;
}
int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d%d",&n,&m);
num=;
for(i=;i<n;i++)
scanf("%s",&ch[i]);
for(i=;i<n;i++)
for(j=;j<m;j++)
if (ch[i][j]=='#')
{
bfs(i,j);
num++;
}
printf("%d\n",num);
}
return ;
}
HDU2952:Counting Sheep(DFS)的更多相关文章
- HDU-2952 Counting Sheep (DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- HDU 2952 Counting Sheep(DFS)
题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...
- Hdu2952 Counting Sheep 2017-01-18 14:56 44人阅读 评论(0) 收藏
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 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- Counting sheep...
Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
随机推荐
- hdu 1210 Eddy's 洗牌问题
Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n.这也是 ...
- Chapter 16_0 面向对象编程
Lua中的table就是一种对象. 1.table和对象一样拥有状态 2.和对象一样有一个独立的标识符(a self) 3.和对象一样,具有独立于创建者和创建地的生命周期. 对象有他们自己的操作,ta ...
- OAuth流程
简介 OAuth是一种协议,OAuth协议为用户资源的授权提供了一个安全的.开放而又简易的标准 第三方若想访问用户资源,就必须遵守服务提供商实现的OAuth协议 OAuth授权的步骤(新浪微博为例) ...
- Just do it!!!
从今日起,开个开发自己个人轻量级博客,加油!!!!!
- asp 操作 xml
'创建DOM对象 set objDom=server.CreateObject( "MicroSoft.XMLDom ") '取得xml数据 '方法1 取得xml文件的xml数据 ...
- MyBatis 错误:Invalid bound statement (not found)
错误: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zr.msgg.per ...
- 浅谈 zookeeper 原理,安装和配置
当前云计算流行, 单一机器额的处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,那么这些信息如何推送到各个节点?并且 ...
- wordpress建站过程1
使用wordpress我们需要理解一些概念: 1.WordPress是一种使用PHP语言开发的博客平台,它的程序是由php构成的,所以想要使用word press必须会php. 2.Wordpress ...
- ZOJ 2158 POJ 1789 Truck History
最小生成树,主要是题目比较难懂. #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- Apriori算法-位运算-C语言
原文地址:http://blog.csdn.net/liema2000/article/details/6118423 //////////////////////////////////////// ...