Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
         

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure
out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. 



Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M 



* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS: 



There are three ponds: one in the upper left, one in the lower left,and one along the right side.

Source

这应该是搜索里面的简单题了吧,和NYOJ水池数目一模一样,刚学搜索时还不会做,然后看到大牛写的博客上说这是最简单的搜索题了。。。

思路就是用深搜,其实广搜深搜都可以,往八个方向找,只要是W则继续搜索并将该点变为‘.’,做法很多种,看代码;

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
//const int INF=0x3f3f3f3f;
const int N=150;
char a[N][N];
int n,m;
int dx[8]= {0,0,1,-1,1,1,-1,-1};
int dy[8]= {1,-1,0,0,-1,1,1,-1};
void dfs(int x,int y)
{
if(x<n&&x>=0&&y>=0&&y<m)
{
a[x][y]='.';
for(int i=0; i<8; i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(a[xx][yy]=='W')
dfs(xx,yy);
}
}
}
int main()
{
int i,j,sum;
while(~scanf("%d%d",&n,&m))
{
for(i=0; i<n; i++)
scanf("%s",a[i]);
sum=0;
for(i=0; i<n; i++)
for(j=0; j<m; j++)
if(a[i][j]=='W')
{
dfs(i,j);
sum++;
}
printf("%d\n",sum);
}
return 0;
}

搜索一定要注意的是边界问题,还有是否会陷入死循环;

最新文章

  1. 连接池的实现 redis例子
  2. hadoop 完全分布式 下 datanode无法启动解决方法
  3. 20.cocoapods的安装和使用
  4. ORA-01034: ORACLE not available如何解决
  5. zepto源码--插入节点--学习笔记
  6. Java中断言的使用(转)
  7. 自学HTML5难 我们应该怎么做
  8. DirectX11 With Windows SDK--07 添加光照与常用几何模型
  9. java.lang.Boolean 类源码解析
  10. python,运算符,基本数据类型
  11. [MySql]GRANT权限的一些技巧
  12. python面试1-30题
  13. day 54 jQuery, part-1
  14. Emgucv - 下载、安装、配置
  15. centos7 pgpool+postgresql
  16. hihocoder-1415 后缀数组三&#183;重复旋律3 两个字符串的最长公共子串
  17. Java中的 JDK下载和环境配置(方式一)
  18. Claim-Based Identity for Windows: Technologies and Scenarios
  19. PHP的XML Parser(转)
  20. 高效率JavaScript代码的编写技巧

热门文章

  1. php 缩略图
  2. RHEL 6.5----LVS(DR)
  3. Windows下降权MYSQL和apche的运行级别(普通用户权限运行)
  4. EmitMapper系列之二:EmitMapper的使用小结
  5. C# 部分命名规则
  6. 模板方法模式及php实现
  7. CSData
  8. 【Win32汇编】编译环境配置
  9. 爬虫学习之第一次获取网页内容及BeautifulSoup处理
  10. manjaro利用docker使用MySQL