I have a CS problem which is stated like that:
Given a sheet of paper with some cells cut out, represented by chars (either '.' (cut) or '#' (not cut)), I need to find how many pieces will form.
Example:
##..#####.
.#.#.#....
###..##.#.
..##.....#
.###.#####
Answer for the example above is 5.
One piece will form:
....
.##.
....
Two pieces will form:
....
.#..
..#.
Common sense suggests me to find a way to represent a sheet with a graph (each number sign is a vertex, and two vertices are connected if they share a side). However, it is unclear to me how to do it. I found out that there are implicit graphs (graphs defined by a function which returns all neighbors of given vertex). A function like this is trivial to implement in the case.
The question is: How should I modify DFS algorithm to find components in this kind of graph?