I am dealing with what amounts to squares on a grid. The grid if of a more or less infinite size. Grid squares can have one of two states, either claimed or unclaimed. The coordinates of the squares are stored as squares in an R tree for fast access. Before a square is unclaimed however, I want to iterate through the nearby claimed squares to ensure that by unclaiming this square, I am not splitting the claimed squares into 2 distinct regions. It’s important to note, that squares are already currently claimable only if they are touching an already claimed square.
So if I have something like
+++++++++
And if a player wants to remove the square to create
++++ ++++
Then I want to be able to detect and reject this action. Of course this needs to be able to handle the full 2 dimensional space as well ( I tried, but found it difficult to get this across in ascii art via the formatting available to me here). This must also be fairly efficient. Any pointers as to the name of an algorithm to look up or a description on how to do this would be wonderful <3
Do breadth-first searches on the neighbors of the removed grid point and see that the searches all meet each other.
Search on the components without the removed grid point until either you exhaust it (the original claimed territory) or meet the other breadth first searches in another location.
For example, if you have the region on the left, and removal results on the right:
(the middle of the top is removed)
now do two breadth first parallel searches: (numbers or letters show number of squares traveled from removed piece)
The connection at x is going to be reached in the 5th step of both breadth first searches, indicating removing the top middle grid does not create two separate regions.