Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41340   Accepted: 20504

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

//深度优先的典型题,大致意思是说找水坑
 #include<iostream>
using namespace std;
char field[][];
int n, m;
void dfs(int i, int j)
{
field[i][j] = '.';
for(int dx=-;dx<=;dx++)
for (int dy = -; dy <= ; dy++)
{
int k = i + dx, kk = j + dy;
if (k >= && k < n&& kk < m&&kk >= && field[k][kk] == 'W')
dfs(k, kk);
}
return;
}
int main()
{
int mark = ;
cin >> n >> m;
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
cin >> field[i][j];
for(int i=;i<n;i++)
for (int j = ; j < m; j++)
if (field[i][j] == 'W')
{
dfs(i, j);
mark++;
}
cout << mark << endl;
return ;
}

真正意义上的第一个深度题,代码不是很难理解,可是感觉掌握起来有点小麻烦;

还需多练习

Lake Counting的更多相关文章

  1. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  2. POJ 2386 Lake Counting(深搜)

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

  3. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  4. bzoj1751 [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 168  Solved: 130 [ ...

  5. BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘

    题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MB Description     农夫 ...

  6. 3385: [Usaco2004 Nov]Lake Counting 数池塘

    3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21 ...

  7. 1751: [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Su ...

  8. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  9. Poj2386 Lake Counting (DFS)

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

  10. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

随机推荐

  1. Spark思维导图之内存管理

  2. 使用Protobuf定义网络协议

    准备工具: 工具下载地址如下:https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1,主要使用到的文件有: protoc.exe ...

  3. python 函数指动态形参,作用域

    函数的动态形参, 作用域 一 动态形参 如果我们需要给⼀一个函数传参, ⽽而参数⼜又是不确定的. 或者我给⼀一个 函数传很多参数, 我的形参就要写很多, 很⿇麻烦, 怎么办呢. 我们可以考虑使⽤用动态 ...

  4. 1Mybatis入门--1.1单独使用jdbc编程问题总结

    1.1.1 jdbc程序 Public static void main(String[] args) { Connection connection = null; PreparedStatemen ...

  5. const的引用

    const的引用 对常量的引用:把引用绑定到const对象上,就像绑定到其他对象上一样,不能被用作修改它所绑定的对象: ; const int &r1 = ci;//正确:引用及其对应的对象都 ...

  6. 判断GPS是否开启&转到设置GPS界面

    /** * 判断GPS是否开启,GPS或者AGPS开启一个就认为是开启的 * @param context * @return true 表示开启 */ public static final boo ...

  7. 关于cmd命令

    F: 直接进入F盘 cd\ 进入当前盘的根目录 md 文件名 创建文件名 cd 文件名 进入文件名 rd  文件名  删除文件夹

  8. 20165234 《Java程序设计》第六周学习总结

    第六周学习总结 教材学习内容总结 第八章 常用实用类 String类 Java专门提供了用来处理字符序列的 String 类.String类在java.lang包中,由于 java.lang 包中的类 ...

  9. flask基本介绍及虚拟环境

    Flask Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎 ...

  10. tomcat线程一直处于RUNNABLE,不接受请求

    最近项目中一个模块出现一个问题,本人做的比较浅显,所以很简单的问题一直搞了好几天,通过各种查资料.工具终于分析除了问题.问题如下: 现在对tomcat一个工程(会通过httpurlconnection ...