C++搜索与回溯算法之Lake Counting(数湖)

这篇博客介绍了如何使用C++进行搜索与回溯算法,特别是解决数湖问题。通过深度优先搜索(DFS)策略,来计算给定二维数组中湖泊的数量。
摘要由CSDN通过智能技术生成

Lake Counting(数湖)

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.

翻译:

由于最近的降雨,水已经在农民约翰的农场中的各个地方聚集,这是由N×M(1 <= N <= 100; 1 <= M <= 100)正方形的矩形表示的。 每个正方形包含水('W')或干地('.')。农民约翰想知道在他的农场已经形成了多少个池塘。 一个池塘是一组连接在一起的水,一个包含水正方形被认为与其邻近的八个方向相邻。
给出了约翰农场的图表,确定他的农场里有多少池塘。

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. 

翻译:

*行1:两个空格分隔的整数:NM

*行2..N + 1:
每行代表一行农民约翰的字段的M 个字符。 每个字符都是“W ”或“ .”。 字符之间没有空格。

Output(输出)

* Line 1: The number of ponds in Farmer John's field.

翻译:

*行1:农民约翰的田野中的池塘数。

Sample Input(样例输入)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值