# What's a clean way to find (up to) 3x3 squares within a 6x6 grid?

I have a 6x6 grid where every cell have a color property (simply integer value).

Player can change one cell at a time to form rectangles of the same color inside of the grid. New rectangles can only be 2x2, 3x2, 2x3 and 3x3 in size.

Is there a cleaner way to find those rectangles rather than having to manually search for [i][j], [i+1][j], [i][j+1] and [i+1][j+1] (et cetera) color matches?

• Have a look at this as it may help en.m.wikipedia.org/wiki/Flood_fill – Savlon Apr 29 '14 at 6:37
• @Savlon A recursive search radiating out from the clicked point is exactly how I would solve this. Flood fill is a great example of a very similar algorithm. – Dan May 12 '14 at 17:50

A potential optimization is to track the state of "partially assembled" squares as they are built. This reduces the number of checks you generally need to make to approximately 8 (the eight surrounding tiles of the clicked tile) to see if the clicked tile should be added to an existing potential solution, and whatever checks you need to see if a potential solution has just become valid (probably two or three more ifs, to check it horizontal and vertical bounds).