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. 通过IIS寄宿WCF服务

    WCF全面解析一书中的例子S104,直接将Service目录部署到iis是无法得到服务相应的,需要在项目中新建一个web项目,删除掉自动生成的所有文件之后,把Service目录下的Calculator ...

  2. Nginx+uWsgi生产部署Django

    部署得过程很简单,部署得核心在于,为什么要这样做,每一个参数代表什么意思,最终的目的是得了解,一些基概念性的东西. uWsgi简介 说Uwsgi之前,先说一下Wsgi. 什么是Wsgi? WSGI: ...

  3. CSS——标准盒子模型

    在写网页的时候一般都先用Div把网页的框架搭好(用不同的背景颜色来区分不同的Div块),然后填充每一个Div,最后把每个Div的背景颜色去掉 <html> <head> < ...

  4. sql server中的大数据的批量操作(批量插入,批量删除)

    首先我们建立一个测试用员工表 ---创建一个测试的员工表--- create table Employee( EmployeeNo int primary key, --员工编号 EmployeeNa ...

  5. UVALive 5840 数学题

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

  6. POJ 3579 median 二分搜索,中位数 难度:3

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3866   Accepted: 1130 Descriptio ...

  7. ReentrantReadWriteLock——写写互斥(二)

    "读写" ."写读"."写写"都是同步的.互斥的 1.Service.java package ReentrantReadWriteLock ...

  8. Java——使用File类递归遍历指定路劲下的所有文件

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  9. L1-010 比较大小

    本题要求将输入的任意3个整数从小到大输出. 输入格式: 输入在一行中给出3个整数,其间以空格分隔. 输出格式: 在一行中将3个整数从小到大输出,其间以“->”相连. 输入样例: 4 2 8 输出 ...

  10. TP 模板的变量输出