当前位置:首页 > 搜索 > 正文
NOI2.5-1388Lake Counting
2907+

题目大意:给你一个n*m的地图,W表示湖水,“.”表示陆地,8个方向有相连的是同一个湖,请问图上共有多少个湖?

题目描述

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 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

样例输出

3

解题思路

Flood Fill算法,地图填充,遍历整个地图,遇到W,累加湖数,并填成非W,搜索该湖其他W,都填成非W;后面搜索只搜索W的,就不会重复搜索。深搜和广搜都可以,时间复杂度O(n*m)。

程序实现

NOI2.5-1388Lake Counting:等您坐沙发呢!

发表评论

您必须 [ 登录 ] 才能发表留言!