博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Openjudge1388 Lake Counting【DFS/Flood Fill】
阅读量:7106 次
发布时间:2019-06-28

本文共 1600 字,大约阅读时间需要 5 分钟。

http://blog.csdn.net/c20182030/article/details/52327948

 

1388:Lake Counting

总时间限制: 
 1000ms 
 内存限制: 
 65536kB
描述
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.
输入
* 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.
输出
* Line 1: The number of ponds in Farmer John's field.
样例输入
10 12W........WW..WWW.....WWW....WW...WW..........WW..........W....W......W...W.W.....WW.W.W.W.....W..W.W......W...W.......W.
样例输出
3
提示
OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
来源
USACO 2004 Novembe
题目大意  
大雨过后,农夫John的农场
被水淹没不知所措有了积水,农场地图中‘W’代表积水,'.'代表干燥的土地。八连通的积水在同一个水洼里。农夫John想知道农场里有多少水洼。
这是一道搜索题。与其他搜索题一样,都是不断地搜索,直到达到目的。但是,这道题的目的是什么呢?
目的是求出有多少水洼,如果递归的话,怎么才能知道求出了数量呢?
所以这样,不能单纯的只调用一次函数(主程序中),否则最多只会找出一个水洼。
我一开始是这样想的,把每个水洼只留下一个W,其余换成干燥,最后再看有几个W。可是这样搜索的时候,留下的W也会被搜到,搜到就会变成'.',答案就会是0。为了避免这个问题,需要设立一个辅助数组或者把这个W换成另一个字符,但是这样既浪费时间也浪费空间(还浪费我的脑容量)。
于是我把函数写在循环里,把一个水洼消灭干净后,水洼数量就加1。我用DFS,BFS实现了这个想法。

 

转载于:https://www.cnblogs.com/Roni-i/p/7821638.html

你可能感兴趣的文章
OpenGL应用函数库介绍
查看>>
常量、枚举
查看>>
条件变量与互斥量
查看>>
Jenkins-Publish HTML reports
查看>>
KVO 键值观察
查看>>
iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
查看>>
Adaptability Is Accessibility
查看>>
HDU_1227_Fast Food_动态规划
查看>>
实验验证redis的快照和AOF
查看>>
临时表的应用
查看>>
码农的福利来了, 编程在线Androd 客户端上线了
查看>>
sys.stdout.write与sys.sterr.write(二)
查看>>
多继承时,多个基类中存在型别相同的虚函数,该怎么做?
查看>>
shell配置,选择,环境变量修改(ORACLE_HOME,ORACLE_SID),无法使用sqlplus
查看>>
Design Hint for Inheritance(继承设计的一些小贴士)
查看>>
java 时间格式化函数
查看>>
python参数
查看>>
P1614 爱与愁的心痛
查看>>
Windows上使用Objective-c和Cocoa
查看>>
android ui事件处理分析
查看>>