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 ...
随机推荐
- Ext tpl 造成 store不能正确加载
最近维护别人写的代码的时候,遇到了这么个情况 找原因找到了这行代码的身上 tpl: '<tpl for="."><div ext:qtip="{name ...
- C#实现在线更新系统
先来看一下程序完成后长什么样. 这个是程序的组成部分. 主要功能是在InitializationUpdate这个类中完成的,From1主要起到调用的作用,所以重心还是在InitializationUp ...
- 写一个Windows上的守护进程(6)Windows服务
写一个Windows上的守护进程(6)Windows服务 守护进程因为要开机启动,还要高权限,所以我就把它做成Windows服务了. 关于Windows服务的官方文档,大家可以看https://msd ...
- perl6的介绍与下载编译安装
遇到perl6 一直想认真的学习一门脚本语言或者与之类似的语言,因为相对与c++/c来说,一些工作可以很方便的用脚本语言来解决,比如对于日志文件的处理,自动ftp上传. 也看过不少语言的介绍,比如py ...
- stretchlim函数分析
在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下 function lowhigh = stretchlim(varargin) %STRETCHLIM Find ...
- php error_log 详解
定义和用法 error_log() 函数向服务器错误记录.文件或远程目标发送一个错误. 成功,返回 true,否则返回 false. error_log(error,type,destination, ...
- Clojure操作mysql
在Eclipse中新建一个Clojure工程clj01 clojure 操作mysql需要依赖mysql-connector-java.clojure-contrib与java.jdbc三个jar包. ...
- Ext4 MVC CRUD操作
项目目录结构如下: (1)index.html <!DOCTYPE html> <html> <head> <title>用户管理</title& ...
- 8051_asm.uew
/L20"8051 Assembly" AASM_LANG Line Comment = ; Nocase String Chars = ' File Extensions = S ...
- 如何为WPF添加Main()函数 程序入口点的修改
一般的.WPF的Main()函数是自动生成的,不过有时候我们需要为我们的应用程序传参.那么自动生成的Main()函数就不会满足我们的要求. 那么如何为WPF Application 设置Main()函 ...