Let's say I have a grid of blocks, 7x12. We use the colors '*','%','@' and an empty cell '-'.
1 2 3 4 5 6 7
- - - - - - - 1
- - - - - - - 2
% % - - - - - 3
% % - - - - * 4
% % - - - @ % 5
@ @ @ - - @ % 6
@ @ * * * - * 7
* * * % % % % 8
% @ @ % * * % 9
% @ % % % % % 10
* * * % % @ @ 11
* * @ @ @ @ * 12
I want to find rectangles in this grid of a certain minimum size, and the biggest I can find and then smaller until no rectangles greater or equal to the minimum size can be found.
In this example, consider the minimum size 1x4, 4x1, 2x2 so a 1x3 is not valid but a 2x3 is. If we want the biggest rectangles we find the following:
- 4x1 at (4,8)
- 5x1 at (3,10)
- 2x3 at (1,3)
- 2x2 at (6,1)
- 2x2 at (1,11)
- 4x1 at (3,12)
Note that rectangles cannot be in each others space, they cannot overlap. For example the 2x2 rectangle at (4,10) is not mentioned because it would overlap the 5x1 rectangle at (3,10).
All are perfectly valid rectangles: they are equal or greater that the minimum size and all the blocks per rectangle are of the same color.
What I want is to do this programmatically. When you tell someone to find rectangles in a grid, he finds them immediatly, without any thinking about it. The question is, how can I write an algoritm that does the same?
I considered bruteforcing but I need the algorithm to execute as fast as possible as it will need to be executed a lot in a very small time frame on a limited (mobile) device.
I see a lot of questions on the internet about rectangles, but I'm suprised this one hasn't been asked anywhere yet. Am I thinking too difficult or has no one ever wanted to do something like this?