G - DFS(floodfill),推荐

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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

其实这道题和UVA 572是一样的,甚至代码都不需要怎么改~~~~
漫水填充,其实就是赋值了,一个连通块赋同一个值,填充完之后答案也就出来咯
例如样例输入中的填充完就是这样:

100000000220
011100000222
000011000220
000000000220
000000000200
003000000200
030300000220
303030000020
030300000020
003000000020

#include"iostream"
#include"cstring"
#include"cstdio"
using namespace std; char a[101][101];
int book[101][101];
int n,m; void dfs(int x,int y,int z)
{
if(x<0||x>=n||y<0||y>=m)
return;
if(book[x][y]>0||a[x][y]!='W')
return;
book[x][y]=z;
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)
if(j!=0||i!=0) dfs(x+i,y+j,z);
} int main()
{
while(scanf("%d%d",&n,&m)==2&&n&&m)
{
// memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%s",a[i]);
memset(book,0,sizeof(book));
int ans=0;
for(int j=0;j<n;j++)
for(int k=0;k<m;k++)
{
if(book[j][k]==0&&a[j][k]=='W') {dfs(j,k,++ans);}
}
cout<<ans<<endl;
}
return 0;
}

DFS求连通块(漫水填充法)的更多相关文章

  1. 用DFS求连通块(种子填充)

    [问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...

  2. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  3. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

  4. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  5. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

  6. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  7. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  8. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  9. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. USACO Training3.3 A Game【区间Dp】 By cellur925

    题目传送门 一股浓浓的博弈论香气...然而本蒟并不会博弈论. 开始用双端队列+假的dp水过了24pts水数据. 其实是布星的,两人都绝顶聪明会深谋远虑不像我只看眼前,所以上述算法错误. 正解:区间dp ...

  2. $Edmonds-Karp$[网络流]

    \(原题戳这里\) >最大流最小割定理$(Maximum Flow, Minimum Cut Theorem): $ 网络的最大流等于最小割 具体的证明分三部分 1.任意一个流都小于等于任意一个 ...

  3. EditextText输入类型

    android:inputType="none"--输入普通字符 android:inputType="text"--输入普通字符 android:inputT ...

  4. _bzoj1208 [HNOI2004]宠物收养所【Splay】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 以后在空间限制允许的情况下我绝对不纠结内存占用问题啦!就因为不舍得用long long ...

  5. 数据结构RMQ

    RMQ算法介绍 RMQ算法全称为(Range Minimum/Maximum Query)意思是给你一个长度为n的数组A,求出给定区间的最值的下标.当然我们可以采用枚举,但是我们也可以使用线段树来优化 ...

  6. android将对象序列化到文件:直接写文件与用Serializable接口的对比

    1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...

  7. 银联手机支付控件官方使用指南(ios版)

    目录 版本信息... 2 目录      3 1       概述... 1 2       支付流程介绍... 1 3       测试帐号... 2 4       iOS客户端... 3 4.1 ...

  8. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  9. 日常记录-代码中Background后Padding 失效

    近日,在开发过程中 遇到了 Layout 代码中设置 Background 后,padding失效的问题,只是在Android 4.4.4 和 4.4.2 的手机上遇到了. 网上搜索了下,说是 4.4 ...

  10. QML中使用相对路径

    QML里有三种路径: 默认使用URL路径. "qrc:///filepath".这用来索引资源文件. "file:///绝对路径".这用来索引本地文件系统中的文 ...