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. 向dnsrecord.txt 中添加 配置

    #!/bin/bash Ip_addr=`cat /etc/dnsrecord.txt |awk '{print $1}' |head -1` Check_dns_url(){ grep $1 /et ...

  2. Simple Recurrent Unit,单循环单元

    SRU(Simple Recurrent Unit),单循环单元 src/nnet/nnet-recurrent.h 使用Tanh作为非线性单元 SRU不保留内部状态 训练时,每个训练序列以零向量开始 ...

  3. package-lock.json和package.json区别

    package.json里面定义的是版本范围(比如^1.0.0),具体跑npm install的时候安的什么版本,要解析后才能决定,这里面定义的依赖关系树,可以称之为逻辑树(logical tree) ...

  4. MySql cmd下的学习笔记 —— 有关select的操作(order by,limit)

    先选择goods表 set names gbk; 把本店价从低到高排序(order by) (注意)order by 要放在where, group by, having之后 查询本店价从高到低排序 ...

  5. OVS-----CentOS7.2上安装OVS软件

    1.安装依赖包: yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config yum -y ...

  6. 多次使用图片物理路径提示占用进程,用流读取设置FileShare权限解决

    //用流读取图片 清空之后再次关联地址 //设置FileShare.ReadWrite权限之后 不会提示占用进程异常 item是物理路径DiscussionImageList = new Interv ...

  7. “指定的参数已超出有效值的范围”在【 parameterUpdate.Add(new OracleParameter("STATUS", 0));】报错

    改成:parameterUpdate.Add()); 就不报错,并不能知道为什么,有知道为什么的,评论告诉我. /// <summary> /// 插入数据 /// </summar ...

  8. Linux安装JDK(tar)

    我以JDK1.8为例 ⒈下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  9. Faster_RCNN 4.训练模型

    总结自论文:Faster_RCNN,与Pytorch代码: 本文主要介绍代码最后部分:trainer.py  .train.py , 首先分析一些主要理论操作,然后在代码分析里详细介绍其具体实现.首先 ...

  10. k64 datasheet学习笔记50---GPIO

    1.前言 GPIO模块支持8bit 16bit 32bit访问,当被配置为GPIO功能时,GPIO数据方向和数据寄存器控制了每个引脚的输出方向和输出数据 不管GPIO配置为何种功能,GPIO输入寄存器 ...