Searching for the biggest area inside 2D matrix I have an algorithmic problem where I need to find the biggest area of a certain type of pixels inside a 2D matrix with following conditions:


Each pixel needs to be connected either diagonally or adjacently.
The area is considered coherent only if it is surrounded by the other type of pixel( x-es on the edge are not)


A pixel is considered an object with 3 fields:

int x,y;          //Cordinates of a pixel
String type;      //Type of a pixel
boolean visited;  //Was the pixel visited


The input file is something like this:

oooooooooo 
oXXooXXXoo
oooXoooooo
xooooXXXXo
xxooXooooo
xxxooooooo


The output in this example should be an int of value 5.

Is someone able to tell me if the BFS algorithm is a viable solution or should I try a different approach?
A BFS combined with floodfill should be enough.

Basically, you do the following:


Iterate until you find the first X
Run a BFS on it and change all pixels to visisted = True. Also keep track if you happen to reach the boundary or fulfill any other criterion s.t. it is disqualified
If it is not disqualified, save its size as the current max-size
Iterate further until you find the next unvisited pixel and redo steps 2-4. Keep track of the largest size found.
If you need the original array, change back the visisted state of each pixel in a final iteration.


Overall runtime: Linear, because you change each element at most twice (once to visisted and once back), and also consider only a constant number of neighbors for each element (8).