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. pycharm快捷键及一些常用设置(转载)

    转载于:http://blog.csdn.net/wangtong95/article/details/51100872 在PyCharm /opt/pycharm-3.4.1/help目录下可以找到 ...

  2. [ZPG TEST 108] Antimonotonicity【贪心】

    T2:Antimonotonicity (Antimonotonicity.pas/in/out 128M 1s) 给你1-N的一个排列,数列中的数字互不相等,要求找出最长的子序列a,满足a1> ...

  3. HDFS执行getDatanodeReport时权限不足的解决办法

    通过JAVA获取HDFS的getDatanodeReport方法时,报权限不足的错误信息. org.apache.hadoop.ipc.RemoteException(org.apache.hadoo ...

  4. 员工管理系统(集合与IO流的结合使用 beta3.0 BufferedReader / ObjectOutputStream)

    Employee.java package cn.employee_io; public class Employee { private String empId; private String n ...

  5. SPFarm.local返回值为null

    创建了一个控制台应用程序,想输出SP2010服务器场下所有对象模型信息,结果:SPFarm.local返回值为null. 经查询解决方法: 1 .net framework版本要使用3.5: 2 目标 ...

  6. hihocoder offer收割编程练习赛12 D 寻找最大值

    思路: 可能数据太水了,随便乱搞就过了. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  7. node.js学习笔记(1)

    一.     安装以及环境配置 安装路径 http://nodejs.cn/download/ 多种环境选择 环境变量的配置 Step1 先检查环境变量中的系统变量里面的path,查看是否加入了nod ...

  8. Java集合框架源码(三)——arrayList

    1. ArrayList概述: ArrayList是List接口的可变数组的实现.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类还提供一些方法来操作内部 ...

  9. mysql 的 case when then 用法 和null 的判断

    表:一个表 aa 有两个字段 id 和 sex ,第1条记录的sex 为空串  ('')  第二条记录的sex 为空  (null) 1. 用法: 第一种: select (case 字段名  whe ...

  10. 图解 TCP/IP 第六章 TCP与UDP 笔记6.1 传输层的作用

     图解 TCP/IP  第六章 TCP与UDP   笔记6.1 传输层的作用   传输层必须指出这个具体的程序,为了实现这一功能,使用端口号这样一种识别码.根据端口号,就可以识别在传输层上一层的应用程 ...