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 ;
}

最新文章

  1. Python文件使用“wb”方式打开,写入内容
  2. delegate和protocol
  3. docker on centos
  4. 【BZOJ 1875】【SDOI 2009】HH去散步
  5. c#如何使用正则表达式ZZ
  6. Android多媒体分析-通过MediaStore获取Audio信息
  7. CentOS 6.4 编译安装LLVM3.3,Clang和Libc++
  8. mysql备份数据库
  9. TCO14 2C L2: CliqueGraph,graph theory, clique
  10. mac下安装android-sdk
  11. Eclipse+Android开发:Android模拟器快捷键
  12. 【emWin】例程二十七:窗口对象——Listview
  13. OC仿支付宝输入UITextField输入车牌号
  14. 企业BI系统应用的切入点及五大策略
  15. Babel(抽象语法树,又称AST)
  16. 搞不懂为什么开发人员爱iOS恨Android?
  17. Android 常用算法
  18. 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具
  19. webpack 踩的坑
  20. Python中utf-8与utf-8-sig两种编码格式的区别

热门文章

  1. JQuery实现可直接编辑的表格
  2. 启动Memcached报错:/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.1.so.6: cannot open shared object file: No such file or directory
  3. mac下zsh的使用:主题、z命令
  4. poj 2284 That Nice Euler Circuit 解题报告
  5. Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)
  6. C语言:用字符读取流和输出流来读写入数据。(文本文件)
  7. java类过滤器,防止页面SQL注入
  8. [JSP]JSP中include指令和include动作的差别
  9. (转)在ios android设备上使用 Protobuf (使用dll方式)
  10. [Backbone] Customzing Backbone