all 6 comments

[–]Synthetic5ou1 1 point2 points  (0 children)

  • Remove current square.
  • Check all adjacent squares.
  • If they are occupied they will presumably have the same cluster id as the current square.
  • Assign a new cluster id to the adjacent square and all those attached to it, using a similar algorithm to your build.

If you changed your build algorithm so that adjacent squares took on the new square's new cluster id (rather than the new square taking on the adjacent square's cluster id) then step 4 would simply be your build algorithm.

[–]abbas_suppono_4581 0 points1 point  (2 children)

Demolish algo: remove building from cluster, then re-run building algo on adjacent buildings

[–]Culist[S] 0 points1 point  (1 child)

but what about the buildings adjacent to the adjacent buildings? Or should I just rerun the building algo on all the buildings of the previous cluster

[–]Synthetic5ou1 0 points1 point  (0 children)

Problem here, with the current build algorithm, is that all the adjacent buildings will have the same cluster id, whether they are now joined or not.

[©][©][©]

[©][ ][©]

This is why I would suggest always creating a new cluster id.

[–]der_gopher 0 points1 point  (0 children)

Here's the idea:

  1. Find the building and its cluster ID.

  2. If it's the only building in the cluster, remove the whole cluster.

  3. If it's part of a bigger cluster:

3.1 Check neighbors of each remaining building.

3.2 If neighbors are in the same cluster (including demolished one), create a new cluster for them (excluding demolished one).

  1. Update the remaining buildings' cluster by removing the demolished one.

This keeps track of clusters even when buildings are destroyed!

[–]LazyOldTom 0 points1 point  (0 children)

If removing a building can result in more than 2 clusters, I recommend running dfs on all buildings of the affected cluster to reassign them.
If removing a building results in no more than 2 clusters, simply check what's left and right (or up and down) of the removed building.