I have a group of squares on a 2d grid, here's an example
I need an algorithm to find out whether we are able to remove a square while keeping the entire group connected. Below is an image where I colored squares that are possible to remove in green, and squares that would make the group disconnected if removed in red.
My current method is to remove the square from the group, and run a flood fill algorithm starting from an arbitrary square to see whether the group is connected or not. If the number of squares retrieved from the flood fill is less than the total number of squares for the group, we know that the group is not connected, in 2+ pieces. This is not very performant and it is better to be able to know whether a square is removable before actually removing it.
I noticed that we can tell whether the square is removable by just its surrounding 8 squares. Edit: As @KilianFoth pointed out in a comment, this is not always true. However, in my case I can ensure that there is no longer path connecting figure 1 for example.
Here are several examples, centered on the square in question.
I can create a list of booleans representing whether each surrounding square is in the group. It could be in the form [north, ne, e, se, s, sw, w, nw] going clockwise around, so number 6 on the image above would be [False, True, True, False, False, False, True, True]. I need a function/algorithm that can take in one of these lists, and output whether the square is removable or not. Here are examples from the above images.
[True, False, False, False, False, False, True, False] -> False (not removable) [True, False, False, False, False, False, True, True] -> True (removable) [True, False, True, False, False, False, True, True] -> False [True, True, True, False, False, False, True, True] -> True [True, True, True, False, True, False, True, True] -> False [False, True, True, False, False, False, True, True] -> False If it works better, the bools can be variables like north or southeast.
My first thought was to check the cardinal directions, and if there are only two squares in our group, then it isn't removable. This works for examples 1, 4, and 6, but gets 2, 3 and 5 wrong. I know the algorithm needs to take diagonals into account, because 1 and 2 are the same only including cardinal directions.
How can I make an algorithm that tells me whether or not a square is removable from the group without causing disconnection?


