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. android--------HttpURLConnection的get,post和图片加载

    URLConnection是个抽象类,它有两个直接子类分别是HttpURLConnection和JarURLConnection.另外一个重要的类是URL,通常URL可以通过传给构造器一个String ...

  2. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

  3. Linux磁盘管理,vi编辑器以及包管理器

    一.Linux磁盘管理 Linux磁盘管理常用的三个命令为df,du,fdisk df:列出文件系统的整体磁盘使用量,利用这个命令来获取磁盘被占用了多少空间,,目前还剩下多少空间用法:df [-ahi ...

  4. shell 命令参数

    $# 是传给脚本的参数个数$0 是脚本本身的名字$1 是传递给该shell脚本的第一个参数$2 是传递给该shell脚本的第二个参数$@ 是传给脚本的所有参数的列表$* 是以一个单字符串显示所有向脚本 ...

  5. 简话Angular 04 Angular过滤器详解

    一句话: filter是万能的数据处理器,可以过滤数据,排序数据,删除数据,扩展数据 1. 内置filter大全 url: https://docs.angularjs.org/api/ng/filt ...

  6. UVALive 4174

    DES:给出一个字符串.连续空格的个数代表一个新的字符.奇数个表示0.偶数个表示1.然后根据这个码作为ASCII码.写出对应的字符.就是统计空格个数.二进制转换成十进制的小模拟.但是比赛的时候敲得很不 ...

  7. learning uboot fstype command

    => fstypefstype - Look up a filesystem type Usage:fstype <interface> <dev>:<part&g ...

  8. create rootfs.img using loop device

    reference: https://www.thegeekdiary.com/how-to-create-virtual-block-device-loop-device-filesystem-in ...

  9. Keil 中文显示乱码解决办法

    在将代码文件转换成UTF-8之前还要把Keil的环境也设置成UTF-8的模式,方法是:“Edit”——〉“Configuration...”——〉“Encoding”,选择“Encode in UTF ...

  10. struts2.1.6 action 01

    目录(?)[-] 安装与设置 HelloWorld 常见问题 Action   struts 官网下载 http://www.apache.org/ http://struts.apache.org/ ...