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. Hbase思维导图之调优

  2. 【webpack】中clean-weabpack-plugin使用方法

    在webpack中打包的文件通常是通过hash生成的,如果文件改动,那么打包的文件就会越来越多,如果想清除之前的文件,可以使用clean-weabpack-plugin插件来处理   注意版本号:我使 ...

  3. angular-file-upload 项目实践踩坑

    API文档: https://github.com/nervgh/angular-file-upload/wiki/Module-API 过程中得到昊哥的鼎力帮助,感谢. 需求如下,分别选择多个文件, ...

  4. Python 16 html 基础 jQuery & Javascript研究

    基础内容 DOM操作 javascript探讨 JQuery初识 DOM innerText  ==>获取仅文本 innerHtml  ==>获取全部内容 <div id=" ...

  5. 【Git】Git常见错误

    错误1.fatal: refusing to merge unrelated histories 致命的:拒绝合并不相关的历史 原因:比如我本地分支是V1.0,我现在想要合并远程master分支上的内 ...

  6. 算法 - 链的操作(三) - 返回倒数第k个节点(no.23)

    输入一个链 : 1 -> 3 -> 5 -> 6 -> 8 输入 k  = 2 返回 6 这个节点 python(2.7) def getNode(head, k): if(h ...

  7. nginx 开启静态 gzip 配合 Vue 构建

    在站点配置添加如下代码: location ~* \.(css|js)$ { gzip_static on; } 这是 nginx 的静态 gzip功能,会自动查找对应扩展名的文件,如果存在 gzip ...

  8. MySql cmd下的学习笔记 —— 有关表的操作(对表中数据的增,删,改,查)

    (知识回顾) 连接数据库 mysql -uroot -p111 先建立一个新库 create database test1; use test1; 由于今天的主要内容是表的操作,建立表的详细过程不是本 ...

  9. 在Linux环境下使用Jexus部署ASP.NET Core

    关于如何在Linux中添加ASP.NET Core运行时环境请参考我的上一篇文章,本文章将不再做赘述. 本文章运行环境如下:  (1) 安装独立版Jexus 本教程安装的是独立版的Jexus,独立版的 ...

  10. bug笔记(pc)

    1.如果a标签中的href没有设置,那么点击的这个按钮的时候,这个页面会自动刷新!!! Bug:   <a href=”” class=”btn”></a>类似这种情况,点击a ...