hdu2952Counting Sheep
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath> using namespace std; bool mp[][];
char s[];
bool visit[][];
int q[][],l,r;
int d1[] = {,-,,};
int d2[] = {,,,-}; void bfs(int a,int b)
{
l = r = ;
visit[a][b] = ;
q[r][] = a,q[r++][] = b;
while(l<r)
{
int x = q[l][],y = q[l++][];
for(int j = ;j<;j++)
{
int xx = x+d1[j],yy = y+d2[j];
if(mp[xx][yy]&&!visit[xx][yy])
{
visit[xx][yy] = ;
q[r][] = xx,q[r++][] = yy;
}
}
}
} int main()
{
int z,n,m,i,j,k;
cin>>z;
while(z--)
{
cin>>n>>m;
memset(mp,,sizeof(mp));
memset(visit,,sizeof(visit));
for(i = ;i<=n;i++)
{
scanf("%s",s);
for(j = ;j<m;j++)
{
mp[i][j+] = (s[j] == '#')?:;
}
}
int ans = ;
for(i = ;i<=n;i++)
{
for(j = ;j<=m;j++)
{
if(!visit[i][j]&&mp[i][j]) ans++,bfs(i,j);
}
}
cout<<ans<<endl;
}
return ;
}
hdu2952Counting Sheep的更多相关文章
- 2001. Counting Sheep
After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...
- hdu 3046 Pleasant sheep and big big wolf 最小割
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...
- 【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 ...
- HDU-2952 Counting Sheep (DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- Funny Sheep(思维)
Problem 1606 - Funny Sheep Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 612 Accepted ...
- HDU 3046 Pleasant sheep and big big wolf(最小割)
HDU 3046 Pleasant sheep and big big wolf 题目链接 题意:一个n * m平面上,1是羊.2是狼,问最少要多少围墙才干把狼所有围住,每有到达羊的路径 思路:有羊和 ...
- 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(DFS)
Counting Sheep Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
随机推荐
- linux基础内容学习一:linux下的分区及安装
linux看系统版本信息 uname -a 如果显示为i386,i686则为32位系统,如果为x86_64则为64位 一块硬盘最多可以有四个主分区其中一个主分区可以用一个扩展分区替换,在这个扩展分区中 ...
- Code Complete阅读笔记(一)
代码大全也读了好几个月了,一开始读中文版,到现在慢慢尝试着读原版,确实感受到了"每天进步一点点"的魅力.遗憾的是没有从一开始就做阅读记录,总有不能尽兴和思路不清之感.确实,就像项目 ...
- iOS开发~interface Builder(简称 IB) 界面构建器
1.interface Builder 设置界面 1.1 是什么? 一个可视化的界面编辑工具软件,在xcode4之后整合到了xcode中 1.2 作用? 通过可视化的界面设置,能够少写或不写代码而完成 ...
- linux中运行python
1.在终端中输入: python **.py 2.最爽的方法: emacs中配置python运行环境.
- UVA 11754 Code Feat (枚举,中国剩余定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud C Code Feat The government hackers at C ...
- Java异常基础Exception
异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的众多子类描述各种不同的异常. ...
- Orchard开源ASP.NET MVC CMS简介
项目简介: Orchard CMS 是一个免费的开放源代码内容管理系统,让用户可在 Microsoft ASP.NET MVC 平台上快速创建网站.它基于灵活的扩展性框架构建,使开发人员和定制人员可通 ...
- java的GUI编程
目前有三种创建模版产生应用窗体: 第一种: public class MenuDemo implements ActionListener{ //你的组件(成员变量) public MenuDemo( ...
- HTML基本标签大全
HTML标签 <h#></h#>标题标签<hr/>水平线,单标记<p></p>段落标签  空格<i></i ...
- python对真假的判断方式
一.如下是以下值就认为是假 1.None-->None值 2.False-->False值 3.0-->数值零不管它是int,float还是complex类型 4.'',(),[]- ...