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.

搜索水题,只要往四周一直搜,搜过的改变值就可以,能搜几次就是几个;

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e12+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0); char a[105][105]; bool dfs(int x,int y)
{
// cout<<x<<" "<<y<<endl; if(a[x][y]!='W') return false;
a[x][y]='a';//把经过的位置改变 if(a[x-1][y-1]=='W')//左上
dfs(x-1,y-1);
if(a[x-1][y+1]=='W')//右上
dfs(x-1,y+1);
if(a[x+1][y-1]=='W')//左下
dfs(x+1,y-1);
if(a[x+1][y+1]=='W')//右下
dfs(x+1,y+1);
if(a[x-1][y]=='W')//上
dfs(x-1,y);
if(a[x][y-1]=='W')//左
dfs(x,y-1);
if(a[x][y+1]=='W')//右
dfs(x,y+1);
if(a[x+1][y]=='W')//下
dfs(x+1,y);
return true;
}
int solve(int n,int m)
{
int sum=0;
rep(i,1,n+1)
{
rep(j,1,m+1)
if(dfs(i,j))
sum++;
}
return sum;
}
int main()
{
int n,m;
sf("%d%d%d%d",&n,&m);
mm(a,'.');
rep(i,1,n+1)
{
sf("%s",&a[i][1]);
//pf("1%s\n",&d[i]);
a[i][m+1]='.';
}
pf("%d\n",solve(n,m));
}

D - Lake Counting的更多相关文章

  1. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  2. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  3. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  4. bzoj1751 [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 168  Solved: 130 [ ...

  5. BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘

    题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MB Description     农夫 ...

  6. 3385: [Usaco2004 Nov]Lake Counting 数池塘

    3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21 ...

  7. 1751: [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Su ...

  8. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  9. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  10. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

随机推荐

  1. Open-Source performance testing tools(From other site)

    http://www.appdynamics.com/blog/php/tools-of-the-trade-for-performance-and-load-testing/

  2. EOF多行写入文件防止变量替换

    问题描述 对多个变量及多行输出到文件,存在变量自动替换,当使用cat<<EOF不想对内容进行变量替换.命令替换.参数展开等 问题解决 转义特殊字符如 $ `等 一.对 $·\ 进行转义 c ...

  3. CART决策树(分类回归树)分析及应用建模

    一.CART决策树模型概述(Classification And Regression Trees)   决策树是使用类似于一棵树的结构来表示类的划分,树的构建可以看成是变量(属性)选择的过程,内部节 ...

  4. win8下C盘不能读写的解决方案[zz]

    做系统安全的时候发现了这个/setintegritylevel参数,没有找到更多资料,找到此文,看来这个参数有点神奇哟!我一个同事遇到了这个问题,主要症状:1.C 盘文件不能修改2.C 盘不能新建文件 ...

  5. [svc]对称加密/非对称加密细枝末节-如何做到数据传输的authentication/data integrity/confidentiality(私密)

    对称/非对称/混合加密的冷知识 数据在互联网上传输,要考虑安全性. 讲到安全,要从三方面考虑: 1.authentication 每一个IP包的认证,确保合法源的数据 2.data integrity ...

  6. Gtk-WARNING**:无法在模块路径中找到主题引擎:“pixmap”的解决

    Gtk-WARNING**:无法在模块路径中找到主题引擎:“pixmap”的解决  解决以上问题, 只需要安装 gnome-themes-standard 即可 如果终端中提示:   (gvim:23 ...

  7. java框架篇---Struts2 本地化/国际化(i18n)

    国际化(i18n)是规划和实施的产品和服务,使他们能很容易地适应特定的本地语言和文化的过程中,这个过程被称为本地化.国际化的过程有时也被称为翻译或本地化启用.国际化是缩写i18n,因为我和两端用n字打 ...

  8. java 的nio与io对比

    转:本文并非Java.io或Java.nio的使用手册,也不是如何使用Java.io与Java.nio的技术文档.这里只是尝试比较这两个包,用最简单的方式突出它们的区别和各自的特性.Java.nio提 ...

  9. 【emWin】例程十五:触摸校准实例——五点校准法

    介绍: 该例程介绍如何校准4.3寸.7寸液晶显示屏.校准方法如下: 1.进入以下界面,用户可选择是否进入校准界面进行液晶校准 *点击屏幕任何地方可进入校准界面 *不采取任何操作,几秒钟后会进入触摸测试 ...

  10. 为app录制展示gif

    已同步更新至个人blog:http://dxjia.cn/2015/07/make-gif-for-app/ 在github上好多不错的开源项目展示demo的时候,都是采用了一个gif图片,很生动具体 ...