How to find all the clusters of forest on the map?

advertisements

How to find all clusters of forest on map ? I have simple class cell like (Type is enum {RIVER, FOREST,GRASS,HILL}

class Cell{
   public:
     Type type;
     int x;
     int y
};

and map like vector<Cell> grid. Can anyone suggest me algorithm to create list<list<Cell>> clusters where list contains FOREST cells in same cluster (cluster are set of connected cells - connection can be in eight direction:up,down,left,right,up_right,up_left,down_left,down_right)? I need to find all clusters of forest on map and put every single cluster in list<Cell>.


The algorithm is rather simple and it actually doesn't even depend on the exact definition of what a cluster is. Say you have a predicate cluster(f0, f1) which yields true if f0 and f1 are in the same cluster. All you need to do is to run though the grid and find a forest. If a cell f is a forest, you check if cluster(f, other) for each known forest. If cluster(f, other) yields true you add f to the cluster of other. You continue to check other known forests in other clusters: when you find another cell c in another cluster for cluster(f, c) also yields true, you merge (std::list<Cell>::spice()) the two clusters.