Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40370   Accepted: 20015

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

题意:n行m列中,连在一起没有分开的W块有多少

从任意的W开始,不停地把邻接的部分用 . 代替。一次dfs后与初始的这个W连接的所有W就都被替换成了 . 。因此直到图中不再存在W为止,总共进行的dfs次数就是答案。

8个方向共对应了8种状态转移,每个格子作为dfs的参数至多被调用一次,复杂度为O(8*N*M)=O(N*M)

AC代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
const int maxn=1e3+10;
char ch[maxn][maxn];
void dfs(int x,int y)
{
ch[x][y]='.';
for(int dy=-1;dy<=1;dy++)
for(int dx=-1;dx<=1;dx++)
{
int nx=x+dx,ny=y+dy;
if(nx>=0&&nx<n&&ny>=0&&ny<m&&ch[nx][ny]=='W') dfs(nx,ny);//不断递归,把n*m区域内的W变成.
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>ch[i][j];
int sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ch[i][j]=='W')
{
dfs(i,j);
sum++;
}
}
cout<<sum<<endl;
return 0;
}

POJ:2386 Lake Counting(dfs)的更多相关文章

  1. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  2. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  3. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  4. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  5. POJ 2386 Lake Counting(深搜)

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

  6. Poj2386 Lake Counting (DFS)

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

  7. POJ 2386 Lake Counting (简单深搜)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  8. [USACO10OCT]Lake Counting(DFS)

    很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...

  9. (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)

    题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...

随机推荐

  1. JavaScript权威指南--脚本化HTTP

    知识要点 超文本传输协议(HTTP)规定web浏览器如何从web服务器获取文档和向web服务器发送表单内容,以及web服务器如何响应这些请求和提交.web浏览器会处理大量的HTTP.通常,HTTP并不 ...

  2. apktool 打包解包apk的总结

    1) 不需要另外下载 baksmali-2.1.2.jar, apktool.jar 好像都包含了. apktool d zhanqi.xxx.apk -o zhanqi 2) smalidea-0. ...

  3. Oracle性能诊断艺术-读书笔记(脚本dbms_xplan_output截图-非常好的)

  4. xhost + 的作用

    xhost 是用来控制X server访问权限的. 通常当你从hostA登陆到hostB上运行hostB上的应用程序时, 做为应用程序来说,hostA是client,但是作为图形来说, 是在hostA ...

  5. VirtualBox + Centos 使用NAT + Host-Only 方式联网

    一.准备工作 1. VirtualBox 2. CentOS镜像 备注:我这里准备好了需要下载的文件,有需要的话可以下载一下,分别是VirtualBox-5.1.24-117012-Win.exe,C ...

  6. Oracle12c中性能优化增强新特性之数据库智能闪存

    智能闪存功能最初在XD中引入.从Oracle11.2.0.2开始,除了用于XD存储,还可用于任何闪盘.Oracle12c中,不需卷管理器就可以使用闪盘. 1.  简介 智能闪存在solaris和lin ...

  7. spring cloud学习(四) Fegin 的使用

    Feign 的使用 什么是Feign? Feign : Declarative REST clients. Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创 ...

  8. 《程序员面试金典》习题解答(C/C++)

    一.数据结构 1.数组与字符串 1.1  实现一个算法,确定一个字符串的所有字符是否全都不同.假使不允许使用额外的数据结构,又该如何处理? /* 假设字符集为ASCII字符串,那么字符串至多有256个 ...

  9. Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用

    1.div层的浮动 1)div向左浮动.向右浮动 <!doctype html> <html> <head> <meta charset="utf- ...

  10. UVALive 5840 数学题

    DES:给出三种材料A,B,C每种的个数.然后组合AB,BC,AC的利润.问能获得的最大利润是多少. 开始一点思路都没有.然后发现可以枚举其中两种的个数.那么最后一种就确定了.还是感觉很机智. #in ...