Counting Sheep

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1782    Accepted Submission(s): 1170

Problem Description
A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, for hours and hours. Then one day my grandmother suggested I tried counting sheep after I'd gone to bed. As always when my grandmother suggests things, I decided to try it out. The only problem was, there were no sheep around to be counted when I went to bed.

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
The first line of input contains a single number T, the number of test cases to follow.

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
For each test case, output a line containing a single number, the amount of sheep flock son that grid according to the rules stated in the problem description.

Notes and Constraints
0 < T <= 100
0 < H,W <= 100

 
Sample Input
2 4 4 #.#. .#.# #.## .#.# 3 5 ###.# ..#.. #.###
 
Sample Output
6 3
 
Source
简单的搜索...
代码:

 //简单的搜索
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
const int maxn=;
char map[maxn][maxn];
typedef struct
{
int x,y;
}po;
int dir[][]={{,}, {-,}, {,} , {,-} } ;
int main()
{
int n,m,t,i,j,k,ans;
queue<po>tem;
scanf("%d",&t);
while(t--)
{
ans=;
scanf("%d%d",&n,&m);
for(i=;i<n;i++)
scanf("%s",map[i]);
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(map[i][j]=='#')
{
ans++;
map[i][j]='.';
po st={i,j};
tem.push( st );
while(!tem.empty())
{
po en=tem.front();
tem.pop();
for(k=;k<;k++)
{
if(map[en.x+dir[k][]][en.y+dir[k][]]=='#')
{
map[en.x+dir[k][]][en.y+dir[k][]]='.';
po sa={en.x+dir[k][],en.y+dir[k][]};
tem.push(sa);
}
}
}
}
}
}
printf("%d\n",ans);
}
return ;
}

HDUOJ----2952Counting Sheep的更多相关文章

  1. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  2. 2001. Counting Sheep

      After a long night of coding, Charles Pearson Peterson is having trouble sleeping. This is not onl ...

  3. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  4. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  5. 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 ...

  6. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. Counting sheep...

    Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...

  8. HDU-2952 Counting Sheep (DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  9. Funny Sheep(思维)

    Problem 1606 - Funny Sheep Time Limit: 1000MS   Memory Limit: 65536KB    Total Submit: 612  Accepted ...

  10. hdu2952Counting Sheep

    Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the ceiling, ...

随机推荐

  1. C++中List的用法

    Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() 给list赋值 back() 返回最后一个元素 begin() ...

  2. Steps to configure a FileShare Quorum Witness for Windows Failover Cluster

    Step 1: Step 2: Step 3: Step 4: You must use the wizard to create the file share. Step 5: to make su ...

  3. Remove Nth Node From End of List leetcode java

    题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  4. 实现iframe窗口高度自适应的又一个巧妙思路

    domainA 中有一个页面index.html,通过iframe嵌套了domainB中的一个页面other.html由于other.html页面在iframe中显示,而且其页面内容会动态的增加或减少 ...

  5. JavaScript中将html字符串转化为Jquery对象或者Dom对象

    实例代码: $('<a href="javascript:void(0);" onclick="showUI(this,"4028f65d5d1bb627 ...

  6. PHP json_encode转换空数组为对象

    问题描述: php返回json格式的数据,当返回数据的为数组,且key为字符串时,json化后将返回jsonObject,但是如果是空数组,有可能返回的就是jsonArray,数据结构不一致导致端解析 ...

  7. CentOs中mysql服务器重置root密码方法

    1. 停止mysql: service mysqld stop 2. 编辑/etc/my.cnf,在[mysqld]这行后面加上skip-grant-tables ,并保存 3. 启动mysql: s ...

  8. 微信小程序中用户唯一ID的获取

    折腾到半夜,搞得挺兴奋,总结一下,免得忘了: 1.微信小程序直接获得的是一些简单信息,基本无用 2.用户唯一标识是openid,还有一个unionid是关联多个公众号之类情况下用,我不大关心 3.在g ...

  9. 微信小程序app配置指南

    //app.json页面 { //页面注册,有几个页面都要在pages里面注册 "pages":[ "pages/index/index", "pag ...

  10. [Sass] Level 4: Extend -- Ex

    Better use @extend with % placeholder. Extend is useful when you want to reuse some of you class. Al ...