Lake Counting
原英文描述:
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.
思路:
采用深度优先(DFS)的方法,可以从任意的 W 开始,不停地把邻接的W部分用 ‘.’ 代替。1次DFS后与初始的这个 W 连接的所有 W 就都被替换成了