HDUOJ----2952Counting Sheep
Counting Sheep
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1782 Accepted Submission(s): 1170
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<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 ;
}
最新文章
- Python文件使用“wb”方式打开,写入内容
- delegate和protocol
- docker on centos
- 【BZOJ 1875】【SDOI 2009】HH去散步
- c#如何使用正则表达式ZZ
- Android多媒体分析-通过MediaStore获取Audio信息
- CentOS 6.4 编译安装LLVM3.3,Clang和Libc++
- mysql备份数据库
- TCO14 2C L2: CliqueGraph,graph theory, clique
- mac下安装android-sdk
- Eclipse+Android开发:Android模拟器快捷键
- 【emWin】例程二十七:窗口对象——Listview
- OC仿支付宝输入UITextField输入车牌号
- 企业BI系统应用的切入点及五大策略
- Babel(抽象语法树,又称AST)
- 搞不懂为什么开发人员爱iOS恨Android?
- Android 常用算法
- 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具
- webpack 踩的坑
- Python中utf-8与utf-8-sig两种编码格式的区别
热门文章
- JQuery实现可直接编辑的表格
- 启动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
- mac下zsh的使用:主题、z命令
- poj 2284 That Nice Euler Circuit 解题报告
- Objective-C:除数为0的情况下异常的处理(检测、抛出、捕捉、处理)
- C语言:用字符读取流和输出流来读写入数据。(文本文件)
- java类过滤器,防止页面SQL注入
- [JSP]JSP中include指令和include动作的差别
- (转)在ios android设备上使用 Protobuf (使用dll方式)
- [Backbone] Customzing Backbone